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.
SQLServer DBAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
This will return the first ID
SELECT name, Min(id)
FROM TEMP_Table
GROUP BY name

Open in new window

This will pick a random ID every time
;with a as (
   -- Generate random numbers
   SELECT name, id, ABS(CHECKSUM(NewId())) % 1000 as random_number 
   FROM TEMP_Table), 
b as (
   -- Rank order those random numbers
   SELECT name, id, ROW_NUMBER() OVER (PARTITION BY name ORDER BY random_number) as rank_order FROM a) 
-- Now pick the top one
SELECT name, id FROM b WHERE rank_order = 1

Open in new window

1
Éric MoreauSenior .Net ConsultantCommented:
something like this would work:
SELECT Name, CASE A.count WHEN 1 THEN CAST(A.ID AS VARCHAR(20)) ELSE 'Any ID' END AS ID
FROM (
	SELECT Name, MIN(ID) AS ID, COUNT(*) AS count FROM #TEMP_Table GROUP BY Name
) AS A

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT t1.Name, t1.ID
FROM TEMP_Table t1
WHERE t1.ID = (SELECT TOP 1 t2.ID
                  FROM TEMP_Table t2
                  WHERE t2.Name = t1.Name)

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
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. :)
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

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

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, Vikas, in what your solution is different from Jim's?
0
SQLServer DBAAuthor Commented:
--
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.