Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the second highest sample date

I am trying to get second highest sample date and get the following error, please let me know where am i doing it wrong

(SELECT* FROM resheader WHERE sampledate =
   (SELECT MAX(sampleDate) FROM resheader 
      WHERE sampleDate < (SELECT MAX(sampleDate) FROM resheader)
   )
) as 'Previous Sample'

Error

Msg 116, Level 16, State 1, Line 12 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

like image 446
user5161329 Avatar asked Jun 30 '26 18:06

user5161329


1 Answers

In SQL Server 2012 you can use use the OFFSET FETCH clause to get the second row (after sorting by desired criteria):

SELECT * FROM resheader 
ORDER BY sampledate DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY
like image 164
Salman A Avatar answered Jul 03 '26 11:07

Salman A