SQL No Dup(Distinct)在行上,但忽略一个col

时间:2012-02-02 16:22:11

标签: sql distinct

例如,表格包含三个列和数据

col1  col2  col3
 10    20    30
 40    50    60
 40    50    80

想要选择最后两行的位置,只返回一行,因为col1和col2是相同的。

由于col3不同,

select distinct将无效。

所以输出将是

 10 20 30
 40 50 xx (don't care)

2 个答案:

答案 0 :(得分:6)

由于您只有一个额外的列,因此您可以使用任意MIN / MAX聚合和GROUP BY

SELECT col1,
       col2,
       MAX(col3) AS col3
FROM   YourTable
GROUP  BY col1,
          col2  

更一般地说,如果您的RDBMS支持分析功能,您可以使用

WITH T
     AS (SELECT col1,
                col2,
                col3,
                ROW_NUMBER() OVER (PARTITION BY col1, col2 
                                       ORDER BY col1, col2) AS RN
         FROM   YourTable)
SELECT col1,
       col2,
       col3
FROM   T
WHERE  RN = 1  

答案 1 :(得分:1)

我在SQL Server中这样做了:

-- Setup test data:
declare @table table (
    col1 int,
    col2 int,
    col3 int
)
insert into @table values (10, 20, 30)
insert into @table values (40, 50, 60)
insert into @table values (40, 50, 80)

-- Here's the query:
select col1, col2, cast(min(col3) as varchar(10)) as col3
from @table
group by col1, col2
having count(*) = 1
union all
select col1, col2, 'xx' as col3
from @table
group by col1, col2
having count(*) > 1

我认为这假设你没有重复的行(所有字段都是重复的),否则你的'xx'可能不正确。