Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Distinct values, but only 1 ID

Author  Topic 

xrum
Yak Posting Veteran

87 Posts

Posted - 2010-05-17 : 13:34:38
hi,

i need to get a list of distinct cities from a database and an ID.

Though each record has it's own ID, any ID correcsponding to that city will do...

i tried this:

SELECT distinct city FROM tblEvent ORDER BY city

which gives me the locations as needed, but when I add the ID to the query it gives me everything (not just the distinct locs)

SELECT distinct city , id FROM tblEvent ORDER BY city

how can i get distinct cities out of the database, but only have 1 corresponding ID with it?

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-17 : 13:38:21
SELECT city , MIN(id) AS id
FROM tblEvent
GROUP BY city
ORDER BY city

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

xrum
Yak Posting Veteran

87 Posts

Posted - 2010-05-17 : 13:42:29
thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-17 : 13:47:54


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -