Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 December 2nd, 2003, 10:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default returning values with SQL

Is there any way to pull the first 2, most recent records out of a DB using SQL or do I have to use dates and compare the dates?



 
Old December 2nd, 2003, 11:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You'll have to use a comparison on an appropriate date column, because the concept of "recent" does not apply in a relational database. The rows in a table are inherently unordered, so asking for the most "recent" rows is meaningless, unless they are explicitly ordered by a date(time) value.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 2nd, 2003, 11:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks for your reply.

That's what I thought... I just wanted to make sure.

 
Old December 5th, 2003, 04:35 PM
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why can't you do "Select top 2 * from tableName order by last_update_date desc"?

 
Old December 5th, 2003, 04:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by anderson_jim
 Why can't you do "Select top 2 * from tableName order by last_update_date desc"?

What if there is no last_update_date? The OP didn't give us his table structure...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 8th, 2003, 01:45 PM
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,

"last_update"date is just a sample column name. You can just replace it with the actual column name (the "appropriate date column" you refer to in your post). Sorry, thought that was clear.

Jim
 
Old December 8th, 2003, 02:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:"last_update"date is just a sample column name. You can just replace it with the actual column name (the "appropriate date column" you refer to in your post). Sorry, thought that was clear.
Fair enough.

The point I was trying to make was that the concept of "recent" has no meaning in SQL unless there is some appropriate column to order by.

A file system typically appends new records to the end of the file; retrieving the last two such records is easy. The concept here of "recent" is inherent in the fact that records are added to the end: the records are implicitly ordered by time.

A relational database is not a file system, so none of these concepts apply (concepts like "record", "end", "file", etc). It is true that some systems may assign a unique row identifier as rows are inserted or updated, but there may be no guarantee that these identifiers are monotonically increasing with respect to time, which is what I interpret "recent" to mean...

You are correct that if an appropriate ordering column exists, that the SQL Server 'TOP' clause is a way to retrieve the most recent rows; there are other ways as well (and you may need them if your RDBMS doesn't implement TOP.)

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Values getting changed while returning/receiving veeruu .NET Web Services 4 January 17th, 2008 08:54 AM
Returning Values from a Stored Procedure kadjw SQL Server ASP 1 September 13th, 2006 12:08 PM
Returning Decimal values from database hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 October 10th, 2005 08:10 PM
Returning values using a key word sql statement CHen Access 0 February 6th, 2005 07:05 AM
EXEC function not returning values aingalsbe PHP How-To 0 June 1st, 2004 10:03 AM





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