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
 General SQL Server Forums
 New to SQL Server Programming
 last 5 rows

Author  Topic 

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-05-04 : 01:22:20
Dear experts, please guide
How can i find the last 5 rows in a table?



thank you very much

Vinod

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-04 : 01:32:09
last 5 rows based on what?... do u have sample data?
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-05-04 : 01:34:23
generally we will find first 5 rows by using top 1

like that select * from mytable (only last five rows)
how can i do that?

Vinod
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-04 : 01:38:55
same way top 5 but the result set in the descending order
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-05-04 : 02:23:49
but the result is in reverse order

Vinod
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-05-04 : 02:47:30
select top 5 * from mytable order by mycolumn asc

select top 5 * from mytable order by mycolumn desc

it's up to you which you call the "first 5" and the "last 5"


www.elsasoft.org
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-04 : 02:49:28
Hope this will help u...

if u want top 10 then put 10 instead of 5 in the below query...

Select * from table where column1 >=( select top 1 column1 from table a where 5 =
(Select count(distinct(column1)) from table where column1 >= a.column1))
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-05-04 : 03:01:39
Absolutly great pbguy................

superb query

thank you very much

Vinod
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-04 : 04:00:38
Seems you need Pagination
Search for the same here and you will find good articles

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -