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 2005 Forums
 Transact-SQL (2005)
 return name with min ID

Author  Topic 

ComputerMike
Starting Member

18 Posts

Posted - 2010-05-21 : 11:16:44
I have the following table

-- Create Table
Declare @Customers TABLE (ID integer, CustName varchar(20), CustType varchar (20))

-- Load Sample Data in Table
INSERT INTO @Customers VALUES (1, 'Jack','NewClient' )
INSERT INTO @Customers VALUES (2, 'Jill', 'NewClient')
INSERT INTO @Customers VALUES (3, 'Tom', 'NewClient')
INSERT INTO @Customers VALUES (4, 'Kathy', 'OldClient')
INSERT INTO @Customers VALUES (5, 'David', 'OldClient')
INSERT INTO @Customers VALUES (6, 'Kathy', 'OldClient')
INSERT INTO @Customers VALUES (7, 'Kim', 'OldClient')
INSERT INTO @Customers VALUES (8, 'Hoggart', 'JGC')
INSERT INTO @Customers VALUES (9, 'Kate', 'JGC')
INSERT INTO @Customers VALUES (10, 'Kim', 'JGC')


I want to query the table and return the first CustName, based on ID for CustType = 'NewClient'. The query would return (1, Jack, NewClient) since Jack has the lowest ID.

I was trying something like
select min(ID), CustName from @Customers where CustType = 'NewClient'

This should be easy, but am having trouble.

Thanks


vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-21 : 11:25:41
[code]select top 1 ID, CustName from @Customers where CustType = 'NewClient'
order by ID[/code]
Go to Top of Page

ComputerMike
Starting Member

18 Posts

Posted - 2010-05-21 : 11:54:32
Thanks, it was easy!!!! guess I need another cup of coffe
Go to Top of Page
   

- Advertisement -