简体   繁体   中英

Count Unique Combinations of Pairs From 2 Columns That Share A Specific Attribute

Given 2 columns of event participant id and the events:

id  |   event
1   |     A
2   |     A
3   |     A
1   |     B
4   |     B
2   |     C
3   |     C
1   |     D
4   |     D
1   |     E
2   |     E
4   |     E

I am hoping to count occurrences of all possible, unique combination co-event-participant pairs of two, which is something similar to:

pair    |    times_co_participate |  co_events
1, 2    |           2             |     A, E
1, 3    |           1             |     A
1, 4    |           3             |     B, D, E
2, 3    |           2             |     A, C
2, 4    |           1             |     E
3, 4    |           0             |     null

The id pair can be in 2 separated columns of id1 and id2, the ultimate goals is finding the pair with the highest co-participate occurences

Use a self join and aggregation:

select t1.id, t2.id, count(*), array_agg(event) as events
from t t1 join
     t t2
     on t1.event = t2.event and t1.id < t2.id
group by t1.id, t2.id

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