views:

87

answers:

2

Let's say I have a query result that looks as follows:

ID    NAME    Phone
----  ----    -----
1     Bob     111-111-1111
1     Bob     222-222-2222
1     Bob     333-333-3333
2     Stan    555-555-5555
3     Mike    888-888-8888
3     Mike    777-777-7777

I want to return just a single row instance of each ID value. It doesn't matter to me which one of the multiple rows I get - the first in the set is ok.

So a possible result would be:

ID    NAME    Phone
----  ----    -----
1     Bob     111-111-1111
2     Stan    555-555-5555
3     Mike    888-888-8888
+5  A: 

For SQL Server [edit] and MS Access, you can do

SELECT [ID], [Name], MIN(Phone) as PhoneNumber
FROM PhoneNumbers
GROUP BY [ID], [Name]

This will return

ID    NAME    Phone
----  ----    -----
1     Bob     111-111-1111
2     Stan    555-555-5555
3     Mike    777-777-7777

You might want to add some sort of unique key to the table, just a thought.

LittleBobbyTables
@LittleBobbyTables - Thanks. This isn't a table but rather a result from a previous query. I am going to quickly try this out...
Howiecamp
@LittleBobbyTables - works great, thank you.
Howiecamp
Alternatives to Min() are Max(), First() and Last(). The latter two will return different data based on the sort order.
David-W-Fenton
+1  A: 

We can probably also help you in a direction, if you provide us with the T-SQL statement that gave you the first results you got, with some information how that can be re-written to get the results you wanted.

Arild R
@Arild - I guess I was thinking it shouldn't matter since I could arrive at this result set in multiple different ways. I really only need to solve from this point forward.
Howiecamp