I have a table with two columns. First name,second name. There are 100 rows in that. No keys on this table.
I want last 30 rows . I can do this by cursors. How it can be solved through normal query.
I have a table with two columns. First name,second name. There are 100 rows in that. No keys on this table.
I want last 30 rows . I can do this by cursors. How it can be solved through normal query.
Use TOP, with the reverse order.
SELECT TOP (30) *
FROM Table
ORDER BY LastName DESC, FirstName DESC;
Overall I agree with Rob Farley, but in case you want to preserve original order, you might want to use the following version:
SELECT *
FROM
(
SELECT TOP (30) *
FROM YourTable
ORDER BY LastName DESC, FirstName Desc
)
ORDER BY LastName ASC, FirstName Asc
or if you don't like subqueries:
SELECT TOP (30)
*,
ROW_NUMBER() OVER (ORDER BY LastName DESC, FirstName DESC) as RowNum
FROM YourTable
ORDER BY RowNum DESC
2 People are following this question.