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 2000 Forums
 SQL Server Development (2000)
 MIN of TOP

Author  Topic 

flamz
Starting Member

21 Posts

Posted - 2008-02-27 : 16:43:44
Hi, I'd like to get the minimum date from a SELECT query that uses TOP but I can't get it to compile:

SELECT MIN(DATE) FROM (SELECT TOP 3 * FROM MyTable WHERE recId=1)

flamz
Starting Member

21 Posts

Posted - 2008-02-27 : 16:49:10
never mind, got it:
SELECT MIN(DATE)
FROM MyTable
WHERE DATE IN
(
SELECT TOP 10 DATE
FROM MyTable WHERE recID =10
) AND recID =10
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-28 : 00:54:31
quote:
Originally posted by flamz

Hi, I'd like to get the minimum date from a SELECT query that uses TOP but I can't get it to compile:

SELECT MIN(t.DATE) FROM (SELECT TOP 3 * FROM MyTable WHERE recId=1)t


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-28 : 00:57:16
And don't forget the ORDER BY to get consistent result with TOP.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-28 : 02:17:58
quote:
Originally posted by flamz

Hi, I'd like to get the minimum date from a SELECT query that uses TOP but I can't get it to compile:

SELECT MIN(DATE) FROM (SELECT TOP 3 * FROM MyTable WHERE recId=1)



Refer method 4 and dont forget to read all comments
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -