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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With