Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从共享特定属性的两列中计算对的唯一组合_Sql_Database_Postgresql_Psql - Fatal编程技术网

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

Sql 从共享特定属性的两列中计算对的唯一组合,sql,database,postgresql,psql,Sql,Database,Postgresql,Psql,给定两列事件参与者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 我希望统计所有可能的、独特组合的两对共同活动参与者的出现次数,这类似于: pair | times_co_participate | c

给定两列事件参与者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
我希望统计所有可能的、独特组合的两对共同活动参与者的出现次数,这类似于:

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

id对可以位于id1和id2的两个独立列中,最终目标是找到具有最高共同参与发生率的id对使用自连接和聚合:

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
选择t1.id、t2.id、count(*)、array_agg(事件)作为事件
从t1连接
t2
在t1.event=t2.event和t1.id
这正是我需要的!我能马上解释一下吗?谢谢@天顶它看起来像一个自连接。我有很多处理数据的经验。