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
 Transact-SQL (2000)
 Selecting the 2nd largest number?

Author  Topic 

TurdSpatulaWarrior
Starting Member

36 Posts

Posted - 2005-08-26 : 15:57:42
Good day all. I have arrived at somewhat of a Friday mental block and cannot seem to get the old cogs turning well enough to figure out something that sounds o simple to me in concept:

Given a table containing a column of varying numeric values, how would one go about selecting the second largest number only?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-08-26 : 16:00:14
SELECT MAX(Column1)
FROM Table1
WHERE Column1 < (SELECT MAX(Column1) FROM Table1)

Tara
Go to Top of Page

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-08-26 : 16:22:43
quote:
Originally posted by tduggan

SELECT MAX(Column1)
FROM Table1
WHERE Column1 < (SELECT MAX(Column1) FROM Table1)

Tara



Tara,

That logic is too cool----are you sure you're not a Vulcan?

Semper fi,
XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!
Go to Top of Page

kapilarya
Yak Posting Veteran

86 Posts

Posted - 2005-08-28 : 04:57:43
try this

delcare @N int
set @N = 2
Select *
From Employee E1
Where (@N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where E2.Salary > E1.Salary)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-29 : 01:11:30
Other approach

select min(col) from (select top 2 col from yourTable order by col desc) T

Madhivanan

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

- Advertisement -