Question

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.

Était-ce utile?

La solution

;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

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top