如何为两列选择不同的值并返回所有列



我想从两列中选择不同的值。

示例数据:

ID TITLE SOURCE TARGET
1  asd   12      2
2  asd1  123     125
3  asd1  123     56  
4  asd2  123     125
5  asd3  164     146

我想获得源列和目标列ID-2和ID-4的不同数据是重复的。

ID TITLE SOURCE TARGET
1  asd   12      2
2  asd1  123     125
3  asd1  123     56  
5  asd3  164     146

如果您只想要不同的值,请使用select distinct:

select distinct source, target
from example t;

如果你想要源/目标只出现在一行的行,那么有一种方法使用窗口函数:

select t.*
from (select t.*,
count(*) over (partition by source, target) as cnt
from example t
) t
where cnt = 1;

最新更新