BendOverIGotYourBack
asked on
Select Next 5 records after TOP 10
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
Simplified Table Structure
ID
Title
Date
Current Query
SELECT TOP 10 *
FROM tblNews
ORDER BY date DESC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep, that's close to this: https://www.experts-exchange.com/questions/20276562/Return-Records-3-6.html?query=select+top+specific+rows&topics=42
Just out of curiousity...what's the A serve?
ORDER BY date DESC) A
Just out of curiousity...what's the A serve?
ORDER BY date DESC) A
The A is just an alias being assigned to the sub-query and it is required by SQL Server.
ASKER
Mokule, I tried your code but resulted in an error "Incorrect syntance near keyword Order"
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.
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.
Type slow and lose...
>> ORDER BY date DESC) A
This serves to not have the error
Incorrect syntance near keyword Order
:)
This serves to not have the error
Incorrect syntance near keyword Order
:)
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
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
(SELECT TOP 5 * FROM
(SELECT TOP 15 *
FROM tblNews
ORDER BY date DESC)
ORDER BY date ASC)
ORDER BY date DESC
:)