SQL Query question

For simplicity, the table has two columns, ID and StartDate

How do I get the lowest ID of the record(s) having the lowest start date?

something like this below will give me the lowest ID separate to the lowest StartDate, I want the corresponding lowest ID from the records WITH the lowest StartDate.

SELECT
   MIN(ID),
   MIN(StartDate)
FROM
    TestTable
LVL 1
craigdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
select *
from TestTable t
WHERE startDate = (SELECT MIN(StartDate) from TestTable where ID = t.id )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YiogiCommented:
Select Min([ID]) FROM TableName WHERE StartDate = (Select Min(StartDate) From TableName)
0
craigdevAuthor Commented:
hmmm, makes my query really complicated because I have to join to about 6 tables in each part, twice... is there an easier way to do this?
0
YiogiCommented:
Sorry but can't think of an easier way to do what you want. You want the lowest id not of the table but of another subset so you first have to find the subset and then select from the subset. Maybe someone else has a better idea.
0
dqmqCommented:
Save your complex query as a view.  Then substitute the name of the view for TestTable in Yioqi's answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.