如何在 big table postgresql 中按行输出组合值?
How output combinations values in column by rows in big table postgresql?
我有一些大(1.21 亿行)table(id bigint,link_to bigint):
id | link_to
---|--------
1 | 4
2 | 4
3 | 4
5 | 7
6 | 7
并且我需要通过 link_to 值链接交叉自我 ID 值。
因此,它应该是具有相同 link_to 值 (4) 的 ID 值 (1,2,3) 之间的所有组合,并对所有 link_to 值重复。
结果应该是:
id | link_to
---|--------
1 | 2
1 | 3
2 | 1
2 | 3
3 | 1
3 | 2
5 | 6
6 | 5
这个 select 我将在同一个 table 中插入(使用 ON CONFLICT DO NOTHING 避免重复唯一索引(id,link_to))。
我尝试使用 GROUP BY link_to -> array_agg(id) -> unnest -> WITH ORDINALITY,但没有成功结果...
还有其他解决方案(CTE、window 函数、自定义函数)吗?
您似乎在寻找自连接:
select b1.id, b2.id
from bigtable b1 join
bigtable b2
on b1.link_to = b2.link_to and b1.id <> b2.id;
我有一些大(1.21 亿行)table(id bigint,link_to bigint):
id | link_to
---|--------
1 | 4
2 | 4
3 | 4
5 | 7
6 | 7
并且我需要通过 link_to 值链接交叉自我 ID 值。 因此,它应该是具有相同 link_to 值 (4) 的 ID 值 (1,2,3) 之间的所有组合,并对所有 link_to 值重复。
结果应该是:
id | link_to
---|--------
1 | 2
1 | 3
2 | 1
2 | 3
3 | 1
3 | 2
5 | 6
6 | 5
这个 select 我将在同一个 table 中插入(使用 ON CONFLICT DO NOTHING 避免重复唯一索引(id,link_to))。 我尝试使用 GROUP BY link_to -> array_agg(id) -> unnest -> WITH ORDINALITY,但没有成功结果...
还有其他解决方案(CTE、window 函数、自定义函数)吗?
您似乎在寻找自连接:
select b1.id, b2.id
from bigtable b1 join
bigtable b2
on b1.link_to = b2.link_to and b1.id <> b2.id;