SQLServer DBA
asked on
SQL query
Hi All,
From table TEMP_Table there are multiple rows with the same name in the NAME column and there is distinct ID for them.I want to fetch one NAME (unique or distinct) and any one ID. Please advise how to write MS SQL query for above scenario.
TEMP_Table data:
NAME ID
-------------------------- ---------- ---------- ---------- --------
A 1049
B 1050
C 1051
C 1052
C 1053
C 1054
C 1055
C 1056
D 1057
D 1058
Required output:
NAME ID
----------------------
A 1049
B 1050
C Any ID
D Any ID
Thanks in Advance.
From table TEMP_Table there are multiple rows with the same name in the NAME column and there is distinct ID for them.I want to fetch one NAME (unique or distinct) and any one ID. Please advise how to write MS SQL query for above scenario.
TEMP_Table data:
NAME ID
--------------------------
A 1049
B 1050
C 1051
C 1052
C 1053
C 1054
C 1055
C 1056
D 1057
D 1058
Required output:
NAME ID
----------------------
A 1049
B 1050
C Any ID
D Any ID
Thanks in Advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good solution Jim.
I don't know why I used a complexed one.
Better, I think I know. Just gave similar solution to another question and got addicted to this one. :)
I don't know why I used a complexed one.
Better, I think I know. Just gave similar solution to another question and got addicted to this one. :)
Hi,
If you want random id from the ID column I think this one liner will do for you..
If you want random id from the ID column I think this one liner will do for you..
SELECT Name, MIN(ID) ID FROM TEMP_Table GROUP BY NAME
OR
SELECT Name, MAX(ID) ID FROM TEMP_Table GROUP BY NAME
So, Vikas, in what your solution is different from Jim's?
ASKER
--
Open in new window
This will pick a random ID every timeOpen in new window