Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to find second maximum record


How to find second maximum record

Author
Message
yuvach_74
yuvach_74
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 26
Pls give me a query for to select second maximum of the result set.

a,b is column names.

insert (a,b)values('2','2012-12-21')

insert (a,b)values('2','2012-12-23')

insert (a,b)values('3','2012-11-23')

insert (a,b)values('3','2012-11-21')

insert (a,b)values('2','2012-12-21')

i need to get the result as

'2','2012-12-21'
'3','2012-11-21'

like that i need to get the result. pls give any suggestion
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
Second maximum as ordered by what?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


yuvach_74
yuvach_74
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 26
order by a required.

i need the result should be as i mentioned
Dennis Post
Dennis Post
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 547
Your question is very unclear.

Maybe you mean something like.
SELECT   MAX(Val)
FROM Table
WHERE Val < (SELECT MAX(Val) FROM Table)





For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.

"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
yuvach_74 (11/14/2012)
order by a required.

i need the result should be as i mentioned


With the sample data you gave, the second max row when ordered by a will not give the results you mentioned.
The second max row ordered by a would return either '2','2012-12-21' or '2','2012-12-23'

Select distinct a, min(b) from table group by a
will give you what you asked, but I suspect it's not what you want.

Perhaps a more detailed explanation and better sample data would help...


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
Heres one solution, using a Row_Number() to select the second record, but it depends on the order by


Declare @t as Table (a int, b date)

insert into @t (a,b)values('2','2012-12-21')

insert into @t (a,b)values('2','2012-12-23')

insert into @t (a,b)values('3','2012-11-23')

insert into @t (a,b)values('3','2012-11-21')

insert into @t (a,b)values('2','2012-12-21')

;With Cte as (
Select *,Row_Number() Over(Partition by a order by b desc) rn from @t
)
Select a,b from Cte where rn=2


;With Cte as (
Select *,Row_Number() Over(Partition by a order by b asc) rn from @t
)
Select a,b from Cte where rn=2



The first one does a Descending sort the second does an ascending sort so you get different values.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search