Home All Groups Group Topic Archive Search About

Most efficient way to write this

Author
7 Jan 2006 12:33 AM
Tim
I have the following:

ID            Name
1000        Smith
1000        Jackson
1000        Fastow
1001        Carr
1002        Michaels

I want the following output: Only distinct ID and first Name
1000        Smith
1001        Carr
1002        Michaels

H

Author
7 Jan 2006 12:02 AM
Tom Moreau
Define "first".  Tables have no order to them.  If you want the highest
alphabetically per ID, try:

select
    ID
,    max (Name) Name
from
    MyTable
group by
    ID

--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com

"Tim" <tim@nospam.com> wrote in message
news:S%Dvf.1348$9F5.698@news.cpqcorp.net...
I have the following:

ID            Name
1000        Smith
1000        Jackson
1000        Fastow
1001        Carr
1002        Michaels

I want the following output: Only distinct ID and first Name
1000        Smith
1001        Carr
1002        Michaels

H
Author
7 Jan 2006 12:02 AM
Jens
The context "First" doesn´t exists if you have no column to order
from. There is no predefined / natural order in the tables. So assuming
that it is makes no matter what name you are getting I use the
aggregate MIN() for this-

SELECT ID,MIN(Name)
FROM SomeTable
GROUP BY Id

HTH, Jens Suessmeyer.
Author
7 Jan 2006 12:06 AM
Jens
The context "First" doesn´t exists unless you use the word ORDER or
another mechanism. There is no predefined / natural order in the
tables. Assuming that you want to get the MIN() name I used this
aggregate for the query:

SELECT Id, MIN(Name)
FROM Sometable
GROUP BY ID


HTH, jens Suessmeyer.
Author
7 Jan 2006 12:08 AM
Trey Walpole
You have distinct ID and name already. (are these first names?)

What makes Smith special for id 1000?
Why that one and not Jackson or Fastow?

and before you say (if you were) "because it's the first in the list" -
this is an rdbms - not a list. there is no such thing as "first" without
a specified order.

Please post DDL so we'll know what other criteria makes Smith special, etc.


Tim wrote:
Show quote
> I have the following:
>
> ID            Name
> 1000        Smith
> 1000        Jackson
> 1000        Fastow
> 1001        Carr
> 1002        Michaels
>
> I want the following output: Only distinct ID and first Name
> 1000        Smith
> 1001        Carr
> 1002        Michaels
>
> H
>
>

AddThis Social Bookmark Button