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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 T-SQL query help

Author  Topic 

dcarva
Posting Yak Master

140 Posts

Posted - 2011-07-14 : 14:34:21
Hello,

I have the following data:

ID Name Location
1 test1 area1
2 test1 area1
3 test1 area1
4 test2 area2
5 test2 area2

I am trying to write a sql statement that will return only the first distinct name and location, regardless of ID. But I still need to return at least one id. Example results:

1 test1 area1
4 test2 area2

I can do this by using table variables and looping to only insert the records I want. But it would save me a lot of time if I could just use a sql statement.

Any help is appreciated!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-14 : 14:48:22
Would this work for you?
select
MIN(Id) as Id,
Name,
Location
from
YourTable
group by
Name,
Location
Go to Top of Page

dcarva
Posting Yak Master

140 Posts

Posted - 2011-07-14 : 14:51:37
WOW! that's it! I cannot believe I missed that. I was trying all kinds of things. Many thanks!


Go to Top of Page
   

- Advertisement -