|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 16, 2012 1:52 AM
Points: 2,
Visits: 16
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 37,651,
Visits: 29,903
|
|
Second maximum as ordered by what?
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP 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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 16, 2012 1:52 AM
Points: 2,
Visits: 16
|
|
order by a required.
i need the result should be as i mentioned
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:26 AM
Points: 109,
Visits: 281
|
|
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. http://www.sqlservercentral.com/articles/Best+Practices/61537/
"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 37,651,
Visits: 29,903
|
|
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 2008, MVP 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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 5:35 AM
Points: 803,
Visits: 2,122
|
|
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
|
|
|
|