Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2004, 10:24 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default Query to Select latest X entries

Hi

I need to display the latest (Top10) entries on my asp page. The data is sorted by ID(PK) thus the latest 10 entries will be from the bottom. Entries contain a dateentered field which is datetime datatype.

How can i return this data by using a single query? The only way I could thought off is do a Select Count, Get the number of records, Subtract 10, Do the query and loop as many times as the result (Count-10) and then only start writing the data... Just like to know if there is an easier way? (Im using sql server)

Thanks in advance
Marnus
 
Old July 5th, 2004, 12:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Marnus,

You don't have to go round the circle to reach there. Instead you can use this.

Select top 10 * from TABLENAME order by COLUMNNAME desc

This gets you the latest 10 records based on the COLUMNNAME that you use in the ORDER BY clause.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 5th, 2004, 12:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The only downside of this method is that your dataset is now ordered in reversed order. Use this query to get the same 10 records, but in ascending order:

SELECT Col1, Col2 FROM MyTable WHERE Col1 IN (SELECT TOP 10 Col1 FROM MyTable ORDER BY Col1 DESC) ORDER BY Col1 ASC

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Distant Voices by Bush (Track 13 from the album: Razorblade Suitcase) What's This?
 
Old July 6th, 2004, 01:41 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

Thanks, Thats working.

Regards
Marnus





Similar Threads
Thread Thread Starter Forum Replies Last Post
Query for getting latest date and following .. dragonball SQL Server 2000 4 February 28th, 2008 08:10 AM
Replacing null table entries in select result? TPP SQL Server 2005 6 July 11th, 2007 03:10 AM
Select the latest date, then the lowest price. Stuart Stalker SQL Server DTS 2 October 12th, 2006 03:42 AM
two tables, one query, getting the latest for each eln MySQL 2 November 17th, 2003 11:15 PM
Returning ONLY the latest records in a query kilkerr1 Access 3 November 3rd, 2003 01:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.