Groups | Blog | Home
all groups > sql server programming > october 2004 >

sql server programming : SELECT Statement that skips first row


JB
10/26/2004 11:53:08 PM
I am looking for a way to select the top 5 rows in a table (based on date),
however, I want to skip the first row.

The query "SELECT TOP 5 * FROM News ORDER BY CreateDate DESC" would give me
the top five, but I don't want the first row in that query.

The best way to describe this is to use a news example. I have one query
that will select top 1 row based on date created -- we'll call that the
feature story.

I need to be able to get the other 4 stories without having the feature
story coming up in the query - in other words - skip the first row.

Any help would be appreciated.

JB

Derrick Leggett
10/26/2004 11:57:37 PM
WHERE NOT EXISTS??? Use top 5 where not exists top 1.

[quoted text, click to view]

JB
10/27/2004 12:11:08 AM
Derrick,
You'll have to forgive me -- my SQL skills are not quite what I wished they
were. This is the query that I came up with based on your last suggestion,
but it doesn't yield any results.

Here is the query: "SELECT TOP 5 * FROM NEWS_NEWS WHERE NOT EXISTS (SELECT
TOP 1 * FROM NEWS_NEWS)"

What am I missing?

Thanks,
JB
[quoted text, click to view]

Steve Kass
10/27/2004 1:42:16 AM
You can do something like this:


select top 4 *
from (
select top 5
OrderID,
CustomerID,
EmployeeID
from Northwind..Orders
order by OrderID desc
) T
order by OrderID

Be sure you have both ORDER BY clauses - one in the subquery, one in the
main query, with opposite orders.

Steve Kass
Drew University

[quoted text, click to view]
Bonj
10/27/2004 11:11:24 PM
select top 4 * from (select top 5 * from news order by createdate desc) sq
order by createdate asc

or, if you want newest first
select * from (select top 4 * from (select top 5 * from news order by
createdate desc) sq1 order by createdate asc) sq2 order by createdate desc


[quoted text, click to view]

AddThis Social Bookmark Button