Solved

selecting the 2nd highest record?

Posted on 2008-10-14
4
230 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now