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

时间:2021-06-14 16:18:14

标签: sql oracle distinct distinct-values

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

示例数据:

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

1 个答案:

答案 0 :(得分:2)

如果您只想要不同的值,请使用 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;
相关问题