Solved

selecting the 2nd highest record?

Posted on 2008-10-14
4
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

717 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