如何改变下表?

时间:2014-02-12 13:11:49

标签: sql sql-server tsql sqlcmd

我有一张像

这样的表格
**ID Value1  Value2**

 11 AAAA    ZZZZ

 11 A134    ZZZZ

 12 BBBB    YYYY

 12 B222    YYYY

 13 CCCC    XXXX

 13 A134    XXXX

我需要结果如下

ID Value1 Value2

11  AAAA    ZZZZ

12  BBBB    YYYY

13  CCCC    XXXX

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

;with cte as
(
select id,value1,value2,row_number() over(partition by id order by id) as rn from #t
)

select id,value1,value2 from cte where rn=1

See Demo

答案 1 :(得分:0)

我们也可以这样做以获得所需的输出

Select * FROM (
select id,value1,value2,ROW_NUMBER()OVER (PARTITION BY ID ORDER BY Value1)AS RN  from #t

)t
WHERE RN = 1


oR

Select t.ID,t.value1,t.value2 FROM #t t
INNER JOIN (Select  DISTINCT ID,value1,value2 FROM #t GROUP BY id,value1,value2)tt
ON tt.id = t.id AND tt.value1 = t.value1 AND tt.value2 = t.value2
ORDER BY t.value1 ASC,t.value2 ASC