Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gather top 5 results, but not the first

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
Is it possible in SQL to gather the top 5 results but excluding the first one? So I need to actually grab rows 2-6?
 
Sorry, should have said that I am on 2000
 
Code:
SELECT TOP 5 *
FROM (SELECT TOP 6 * FROM YourTable
             ORDER BY SomeField) Tbl1
ORDER BY SomeField DESC
NOT TESTED!

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
thanks for that, I am almost getting what I need but I want them in id order, the newest first but if I order by asc it is still pulling the first one, if I order by desc it shows the 5th newest, followed by forth etc

select TOP 5 * from (select TOP 6 * from table order by id desc) tbl1 order by id

gives

result 6
result 5
result 4
result 3
result 2

but I need them in the order

result 2
result 3
result 4
result 5
result 6


r937 - I could do that, how would I do it in. asp?
 
Code:
SELECT * 
FROM(select TOP 5 *
       from (select TOP 6 * 
                    from table
             order by id) tbl1 
     order by id  desc) Tbl2
order by id


Other way is like markros said:
Code:
select TOP 5 *
       from table
WHERE Id > (SELECT TOP 1 Id FROM Table)
order by id

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
[rofl]
Yes, it wont work w/o ORDER BY
BTW markros also had a typo :)


Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
I missed that :)

I saw this:
Code:
MyField [COLOR=red][b]<[/b][/color](select Top 1 Field order by Field)

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top