简体   繁体   中英

Sql Server - Join on Aggregate IN clause

I'm trying to find a count of transactions where 2 categories are present.

I have 3 categories: chair, table, cup

Which becomes 9 combinations (ignoring dupes and order) using a CROSS JOIN:

Item_1  Item_2
Table   Table
Table   Chair
Table   Cup
Chair   Chair
Chair   Table
Chair   Cup
Cup     Cup
Cup     Chair
Cup     Table

Next there's a table of transaction id's and category at the line level:

Trans_id    Type
123         Table
123         Cup
234         Chair
345         Cup
345         Table

I'm trying to get a table like this showing the 2 categories and the count of distinct transactions with both categories present:

Item_1  Item_2  Count
Table   Table   578
Table   Chair   826
Table   Cup     370
Chair   Chair   235
Chair   Table   736
Chair   Cup     662
Cup     Cup     306
Cup     Chair   262
Cup     Table   839

I thought I'd be able to do something like:

select item1, item2, count(distinct(trans_id))
from t1
join t2
   on t1.item1 IN (SELECT type FROM t2 group by trans_id)
   and t1.item2 IN (SELECT type FROM t2 group by trans_id)

but not having any luck - any suggestions?

You need to join the transactions table twice once for item1 and once for item2. A group by thereafter will get you the counts.

select t2.item1, t2.item2, count(distinct t11.trans_id) 
from t2
join t1 t11 on t2.item1 = t11.type 
join t1 t12 on t2.item2 = t12.type 
where t11.trans_id=t12.trans_id
group by t2.item1, t2.item2

t1 in this query is the transaction table, t2 is the one which has combinations of items.

This query produces symmetric pairs like (table,chair), (chair,table) and each combination will be counted twice for each transaction id. I am not sure if you need that. More clarification needed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM