sql - Select Distinct result on top but return all result -
i have table below structure , data ...
email name address abc@a.com ab abc@a.com de fr xyz@x.com bv dc efg@e.com cd mm xyz@x.com bv dc efg@e.com cd mm
i want display distinct rows based on email on top. example in above case result should be:
email name address abc@a.com ab xyz@x.com bv dc efg@e.com cd mm abc@a.com de fr xyz@x.com bv dc efg@e.com cd mm
i using below query me distinct result , need rows distinct on top
select row_number() over(partition email order email desc) rownumber, email, name address rownumber = 1
the easy way... use row_number in order clause...
if object_id('tempdb..#testdata', 'u') not null drop table #testdata; create table #testdata ( email varchar(10) not null, [name] char(2) not null, [address] char(2) not null ); insert #testdata (email, [name], [address]) values ('abc@a.com', 'ab', 'us'), ('abc@a.com', 'de', 'fr'), ('xyz@x.com', 'bv', 'dc'), ('efg@e.com', 'cd', 'mm'), ('xyz@x.com', 'bv', 'dc'), ('efg@e.com', 'cd', 'mm'); select * #testdata td order row_number() on (partition td.email order td.[name]);
Comments
Post a Comment