Get the second highest sample date
I am trying to get the second highest sampled date and get the following error please let me know where I am doing it wrong
(SELECT* FROM resheader WHERE sampledate =
(SELECT MAX(sampleDate) FROM resheader
WHERE sampleDate < (SELECT MAX(sampleDate) FROM resheader)
)
) as 'Previous Sample'
Mistake
Msg 116, Level 16, State 1, Line 12 Only one expression can be specified in the select list when no subquery is entered with EXIST.
+3
source to share
3 answers
In SQL Server 2012, you can use a clause OFFSET FETCH
to get the second row (after sorting by your desired criteria):
SELECT * FROM resheader
ORDER BY sampledate DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY
+1
source to share