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.
| 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 aFROM table1ORDER BY a ascSELECT TOP 10 aFROM table1ORDER BY a desc[/code]is effectively returning the bottom 10 resultsfrom you original query |
 |
|
|
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..Thanksmousumi |
 |
|
|
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. |
 |
|
|
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 intSet @count=(Select count(*) from Log)select * from Logwhere Log.record>@count-15order by package_Name,record |
 |
|
|
Frank Kalis
Constraint Violating Yak Guru
413 Posts |
|
|
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. |
 |
|
|
|
|
|