Jump to content

How to select the last n records from selected records?


jkusmanto

Recommended Posts

Hi all,I use SQL Server.Here is my SQL (temporary):SELECT TOP 2 *FROM ( SELECT TOP 5 *FROM table1ORDER BY Date1 DESC) xORDER BY Date1 DESCWhat I want is:I select the first 5 rows (descending).From this 5 rows, I just want to get the last 2. How to get them?ex. --> orginal data:02/05/200603/14/200607/30/200608/31/200609/02/200609/17/200611/24/2006The result has to be like this:08/31/200607/30/2006Can anybody help?Thanks,

Link to comment
Share on other sites

Try this....

SELECT	 TOP 2 *FROM		 (SELECT	 TOP 5 *					   FROM		  tbl_Name					   ORDER BY Date1 DESC) tbl_NameORDER BY Date1

Inner query will give 11/24/200609/17/200609/02/200608/31/200607/30/2006and if you "order by" Date1 then it will be ascending order. then TOP 2 will give you the required date.Output:07/30/200608/31/2006

Link to comment
Share on other sites

Try this....
SELECT	 TOP 2 *FROM		 (SELECT	 TOP 5 *					   FROM		  tbl_Name					   ORDER BY Date1 DESC) tbl_NameORDER BY Date1

Inner query will give 11/24/200609/17/200609/02/200608/31/200607/30/2006and if you "order by" Date1 then it will be ascending order. then TOP 2 will give you the required date.Output:07/30/200608/31/2006

You are right, pulpfiction.But I want the opposite of this. Not like the output you mentioned, but:08/31/200607/30/2006Can you help me please?Thanks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...