SQL server 2008 R2, select one value of a column for each distinct value of another column -


on sql server 2008 r2, select 1 value of column each distinct value of column.

e.g.

 name   id_num  tom      53  tom      60  tom      27  jane     16  jane     16  bill     97  bill     83 

i need 1 id_num each distinct name, such

 name   id_num  tom      27  jane     16  bill     97 

for each name, id_num can randomly picked (not required max or min) long associated name.

for example, bill, can pick 97 or 83. either 1 ok.

i know how write sql query.

thanks

select   name,min(id_num) yourtable group name 

update: if want pick id_num randomly, may try this

with cte (   select      name, id_num,rn = row_number() on (partition name order newid())   yourtable ) select * cte rn = 1 

sql fiddle demo


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -