Solved

selecting the 2nd highest record?

Posted on 2008-10-14
4
233 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:billyleo
4 Comments
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 500 total points
ID: 22710203
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
 
LVL 31

Expert Comment

by:James Murrell
ID: 22710205
something like

SELECT TOP 1 *
FROM (SELECT TOP 2*
FROM tablename
ORDER BY columnname DESC)e1
ORDER BY Sal
0
 
LVL 5

Expert Comment

by:muktajindal
ID: 22710258
You may use following query instead :

SELECT MAX(OrderID)
FROM T_orderheader
WHERE OrderID NOT IN
  (SELECT MAX(OrderID)
   FROM T_orderheader)
0
 

Author Closing Comment

by:billyleo
ID: 31505827
Cool, Thx!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question