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 September 17th, 2004, 10:22 AM
Authorized User
 
Join Date: Jul 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default select out last 10 records

hi all
i need to select out the last 10 records for a particular condition, ie
select last_10_records from mytable where user_name = "Karl"
any ideas?
Slán
~M
____
Mark
 
Old September 17th, 2004, 10:26 AM
Authorized User
 
Join Date: Apr 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What database are you using?

Also, note that without an ORDER BY clause in your statement, most databases provide no guarantee of any particular ordering, which makes the "last 10 rows" ambiguous.

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
 
Old September 17th, 2004, 06:55 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

perhaps try something like this:

SELECT TOP 10 tStocks.ID, tStocks.idCompanyID, tStocks.dtTrade
FROM tStocks
WHERE tStocks.idCompanyID=485
ORDER BY tStocks.ID DESC

this obviously depends on the id column being sequential

try to adapt that to your schema, and/or research the key words
"TOP"

might help

conrad
 
Old September 17th, 2004, 09:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Mark,
Code:
select top 10 * from mytable where user_name = "Karl" order by COLUMNAME DESC
ColumnName(use the relavant columnname from your table) marked in red above, decides the order of rows by which you would like to retrieve the last 10 records, as DESC sorts it by descending order and thus lets you get the top 10 records of the descendingly ordered rows.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old September 30th, 2004, 04:45 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

TOP Count(*) - 10 rows are the rows which u don't want. So u can find the TOP Count(*) - 10 rows in a subquery and then select all rows from table where these rows should not be included.

Its not must to sort to do this.If u r sorting the table then, it becomes very easy. If u have a primery key with clustered index on table then also becomes very easy because table is sorted on the PK. But if ur table doesn't have any Clustered index then u can use this logic.


Example >> Suppose u have a Table T1 with Key colum Col1.

Then U can fire this script in a single batch.
--------------------
DECLARE @ROWSNUM NUMERIC
DECLARE @STRQUERY VARCHAR(1000)
SELECT @ROWSNUM = (COUNT(*)-10) FROM T1
SET @STRQUERY='SELECT * FROM T1 WHERE COL1 NOT IN ('
SET @STRQUERY=@STRQUERY+ 'SELECT TOP '+ CAST(@ROWSNUM AS VARCHAR(10))
SET @STRQUERY=@STRQUERY+' COL1 FROM T1 )'
EXECUTE (@STRQUERY)
----------------------
Any sorting order is not required with above. However if ur table doesn't have any unique key to identify a row, its really a bad table design and u have to find out some combination keys to identify a row.

Hope you got my point. By the way this is for SQL Server.

I hope the logic will help u if u r using some other RDBMS.

B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select .. Top 10 by Month? crabjoe SQL Server 2000 17 March 27th, 2008 11:48 AM
Query to retrieve the first 10 records inserted anup_daware Oracle ASP 0 August 6th, 2007 01:50 AM
how i can select this records dhua SQL Server 2000 0 February 8th, 2007 11:56 PM
Possible to select an OWC 10 Pivot Table cell? Kurt.W VBScript 0 September 26th, 2005 08:22 PM
Processing of 10 Million Records kalyan_samaddar Oracle 1 July 9th, 2003 07:50 AM





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