Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
hello i have a table in which there are multiple record. i want to retrive last 7 record from last 7 row in to a single row. how can i write query.

What I have tried:

i have tried sub query but doesnt work
Posted
Updated 12-Oct-16 21:55pm

1 solution

If you are using SQL Server then you can do something like following-
SQL
SELECT coulmn1,column2,...
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY coulmn2 DESC) slno, * --where column2 is the column by which you can distinguish which are last records
    FROM MyTable
) as tbl
WHERE tbl.slno <= 7

Following query should also work but can be time consuming when you have large no of records. Good thing is it should work for all major DBMS.
SQL
SELECT TOP 7 *
FROM MyTable
ORDER BY Column2 DESC


Hope, it helps :)
Please let me know if you still have issue on this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900