[英]How to select distinct values for two and return all columns?
I want to select distinct values from two columns.我想 select 来自两列的不同值。
Example data:示例数据:
ID TITLE SOURCE TARGET
1 asd 12 2
2 asd1 123 125
3 asd1 123 56
4 asd2 123 125
5 asd3 164 146
I want to get distinct data for source and target columns ID - 2 and ID - 4 are duplicates.我想获得源列和目标列 ID - 2 和 ID - 4 的不同数据是重复的。
ID TITLE SOURCE TARGET
1 asd 12 2
2 asd1 123 125
3 asd1 123 56
5 asd3 164 146
If you just want the distinct values, use select distinct
:如果您只想要不同的值,请使用
select distinct
:
select distinct source, target
from example t;
If you want the rows where the source/target only appears on one row, then one method uses window functions:如果您想要源/目标仅出现在一行上的行,则一种方法使用 window 函数:
select t.*
from (select t.*,
count(*) over (partition by source, target) as cnt
from example t
) t
where cnt = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.