简体   繁体   中英

How to alter the below table?

I have a table like Below

**ID Value1  Value2**

 11 AAAA    ZZZZ

 11 A134    ZZZZ

 12 BBBB    YYYY

 12 B222    YYYY

 13 CCCC    XXXX

 13 A134    XXXX

And I need the result as Below

ID Value1 Value2

11  AAAA    ZZZZ

12  BBBB    YYYY

13  CCCC    XXXX

Please help me on this.

;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

we can do this type also to get required output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM