繁体   English   中英

基于另一列的所有唯一组合

[英]All unique combinations of a column based on another one

我有一个包含以下列的表格:

col1 col2
1     1
1     2
1     1
2     1
3     3
3     2
3     1
3     2
3     4
3     1

我想得到 col2 的每个值的所有组合,除了它本身按 col1 分组,即实现以下 output:

col1 col2 col2_combo
1    1    2
3    3    2
3    3    1
3    3    4
3    2    1
3    2    4
3    1    4

我曾尝试使用表的自连接,但我无法获得唯一的结果,即每两个值组合的一行。

表中有重复项。 所以,第一步是 select 不同的值。 好吧,然后将表连接到自身。

with pairs as (select distinct col1, col2 from mytable)
select a.col1, a.col2, b.col2
from pairs a
join pairs b on b.col1 = a.col1 and b.col2 > a.col2
order by a.col1, a.col2, b.col2;

暂无
暂无

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

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