[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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
0
craigdev
Asked:
craigdev
4 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
select *
from TestTable t
WHERE startDate = (SELECT MIN(StartDate) from TestTable where ID = t.id )
0
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now