简体   繁体   English

从共享特定属性的 2 列中计算对的唯一组合

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

Given 2 columns of event participant id and the events:给定 2 列事件参与者 ID 和事件:

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 id 对可以在 id1 和 id2 的 2 个单独的列中,最终目标是找到具有最高共同参与发生率的对

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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