[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select Next 5 records after TOP 10

Posted on 2005-05-12
9
Medium Priority
?
279 Views
Last Modified: 2010-03-19
I have a table displaying news items.  Currently right now it displays the top 10 most recent items.  What I'd like to do is below that have the next five most recent news items displayed. How would I go about dong this?

Simplified Table Structure
ID
Title
Date

Current Query
SELECT TOP 10 *
FROM tblNews
ORDER BY date DESC
0
Comment
Question by:BendOverIGotYourBack
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 28

Accepted Solution

by:
rafrancisco earned 500 total points
ID: 13990500
Try this:

SELECT TOP 5 * FROM (
SELECT TOP 15 *
FROM tblNews
ORDER BY date DESC) A
ORDER BY Date ASC
0
 
LVL 17

Expert Comment

by:mokule
ID: 13990537
SELECT * FROM
(SELECT TOP 5 * FROM
(SELECT TOP 15 *
FROM tblNews
ORDER BY date DESC)
ORDER BY date ASC)
ORDER BY date DESC

:)
0
 

Author Comment

by:BendOverIGotYourBack
ID: 13990547
Yep, that's close to this: http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20276562.html?query=select+top+specific+rows&topics=42

Just out of curiousity...what's the A serve?

ORDER BY date DESC) A
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 28

Expert Comment

by:rafrancisco
ID: 13990563
The A is just an alias being assigned to the sub-query and it is required by SQL Server.
0
 

Author Comment

by:BendOverIGotYourBack
ID: 13990587
Mokule, I tried your code but resulted in an error "Incorrect syntance near keyword Order"  
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 13990599
Since you have an ID field, you can also use this query:

SELECT TOP 5 *
From tblNews
WHERE ID Not In (SELECT TOP 10 ID
                          FROM tblNews
                          ORDER BY date DESC)
ORDER BY date DESC

I'm not certain which would be faster.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 13990604
Type slow and lose...
0
 
LVL 17

Expert Comment

by:mokule
ID: 13990652
>> ORDER BY date DESC) A
This serves to not have the error
Incorrect syntance near keyword Order
:)
0
 
LVL 17

Expert Comment

by:mokule
ID: 13990671
So correctly it should be

SELECT * FROM
(SELECT TOP 5 * FROM
(SELECT TOP 15 *
FROM tblNews
ORDER BY date DESC) A
ORDER BY date ASC) B
ORDER BY date DESC

It then preservers order
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

868 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