selecting the 2nd highest record?

I need to get the 2nd latest value of my OrderId field.
The code I am using is attached.
Could you pleade tell me why this is a syntax error?

select top 1 OrderID from (select top 2 OrderID from 
T_orderheader order by OrderID desc) 
order by OrderID asc

Open in new window

billyleoAsked:
Who is Participating?
 
tiagosalgadoConnect With a Mentor Commented:
Give a name to your "table".

select top 1 OrderID from (select top 2 OrderID from 
T_orderheader order by OrderID desc) l
order by OrderID asc

Open in new window

0
 
James MurrellProduct SpecialistCommented:
something like

SELECT TOP 1 *
FROM (SELECT TOP 2*
FROM tablename
ORDER BY columnname DESC)e1
ORDER BY Sal
0
 
muktajindalCommented:
You may use following query instead :

SELECT MAX(OrderID)
FROM T_orderheader
WHERE OrderID NOT IN
  (SELECT MAX(OrderID)
   FROM T_orderheader)
0
 
billyleoAuthor Commented:
Cool, Thx!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.