Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Last 10 records in a table

Author  Topic 

mdhingra01
Posting Yak Master

179 Posts

Posted - 2004-03-05 : 16:07:56
Is there function in SQL that will grab the last 10 records in table. Like Select Top 10 A from Table 1, but I wan the bottom 10 records.

Thanks

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-03-05 : 16:12:38
[code]SELECT TOP 10 a
FROM table1
ORDER BY a asc

SELECT TOP 10 a
FROM table1
ORDER BY a desc
[/code]is effectively returning the bottom 10 results
from you original query
Go to Top of Page

smousumi
Starting Member

19 Posts

Posted - 2004-03-09 : 01:07:15
The code given by ehorn is woking fine..
but is there any way to find without sorting the records,
Since sorting may disturb the order of inserting records.
Without sorting, is it possible to fetch last 10 records..
Thanks



mousumi
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-03-09 : 08:05:05
In a relational database, physical order means nothing. Without ordering it by actual values, TOP is meaningless. Unless you have a date column or a sequential number column, you cannot guarantee an order based on when the row was inserted.
Go to Top of Page

mdhingra01
Posting Yak Master

179 Posts

Posted - 2004-03-09 : 09:19:55
Thanks everyone,

I found this code worked the best for me...

Declare @count int
Set @count=(Select count(*) from Log)

select * from Log
where Log.record>@count-15
order by package_Name,record
Go to Top of Page

Frank Kalis
Constraint Violating Yak Guru

413 Posts

Posted - 2004-03-09 : 09:37:35
You want some kind of paging like MySQL's LIMIT?

--Frank
http://www.insidesql.de

Oops!
Go to Top of Page

despy
Starting Member

9 Posts

Posted - 2004-03-10 : 06:29:19
You could go for

SELECT *
FROM Whereever
WHERE MyID IN
(
SELECT TOP 10 MyID
FROM Whereever
ORDER BY MyID DESC
)
ORDER BY MyID ASC


That should get you the last ten records but in the order you require.
Go to Top of Page
   

- Advertisement -