On SQL server 2008 R2, I would like to select one value of a column for each distinct value of another column.
e.g.
name id_num
Tom 53
Tom 60
Tom 27
Jane 16
Jane 16
Bill 97
Bill 83
I need to get one id_num for each distinct name, such as
name id_num
Tom 27
Jane 16
Bill 97
For each name, the id_num can be randomly picked up (not required to be max or min) as long as it is associated with the name.
For example, for Bill, I can pick up 97 or 83. Either one is ok.
I do know how to write the SQL query.
Thanks
UPDATE: If you want pick id_num randomly, you may try this
SQL Fiddle Demo
select name, max(id_num) from [mytable] group by name
The (SELECT 1) in the cte does not really order the data in each of the partitions. which should give you the random selection.
You could grab the max id like this:
That would get you one id for each distinct name.