Quantcast
Channel: SQL Server SELECT LAST N Rows - Stack Overflow
Browsing latest articles
Browse All 21 View Live

Answer by Sara for SQL Server SELECT LAST N Rows

use desc with orderby at the end of the query to get the last values.

View Article



Answer by CodeCowboyOrg for SQL Server SELECT LAST N Rows

A technique I use to query the MOST RECENT rows in very large tables (100+ million or 1+ billion rows) is limiting the query to "reading" only the most recent "N" percentage of RECENT ROWS. This is...

View Article

Answer by Hakan Fıstık for SQL Server SELECT LAST N Rows

In a very general way and to support SQL server here is SELECT TOP(N) * FROM tbl_name ORDER BY tbl_id DESC and for the performance, it is not bad (less than one second for more than 10,000 records On...

View Article

Answer by Slava for SQL Server SELECT LAST N Rows

DECLARE @MYVAR NVARCHAR(100) DECLARE @step int SET @step = 0; DECLARE MYTESTCURSOR CURSOR DYNAMIC FOR SELECT col FROM [dbo].[table] OPEN MYTESTCURSOR FETCH LAST FROM MYTESTCURSOR INTO @MYVAR print...

View Article

Answer by Basil Bourque for SQL Server SELECT LAST N Rows

This may not be quite the right fit to the question, but… OFFSET clause The OFFSET number clause enables you to skip over a number of rows and then return rows after that. That doc link is to Postgres;...

View Article


Answer by timberhill for SQL Server SELECT LAST N Rows

This query returns last N rows in correct order, but it's performance is poor select * from ( select top N * from TableName t order by t.[Id] desc ) as temp order by temp.[Id]

View Article

Answer by Ardalan Shahgholi for SQL Server SELECT LAST N Rows

First you most get record count from Declare @TableRowsCount Int select @TableRowsCount= COUNT(*) from <Your_Table> And then : In SQL Server 2012 SELECT * FROM <Your_Table> As L ORDER BY...

View Article

Answer by mayur godhani for SQL Server SELECT LAST N Rows

Try using the EXCEPT syntax. Something like this: SELECT * FROM clientDetails EXCEPT (SELECT TOP (numbers of rows - how many rows you want) * FROM clientDetails)

View Article


Answer by fth for SQL Server SELECT LAST N Rows

select * from (select top 6 * from vwTable order by Hours desc) T order by Hours

View Article


Answer by Prafulla Sutradhar for SQL Server SELECT LAST N Rows

If you want to select last numbers of rows from a table. Syntax will be like select * from table_name except select top (numbers of rows - how many rows you want)* from table_name These statements work...

View Article

Answer by Dzamo Norton for SQL Server SELECT LAST N Rows

Here's something you can try without an order by but I think it requires that each row is unique. N is the number of rows you want, L is the number of rows in the table. select * from tbl_name except...

View Article

Answer by Niru Mukund Shah for SQL Server SELECT LAST N Rows

You can make SQL server to select last N rows using this SQL: select * from tbl_name order by id desc limit N;

View Article

Answer by abhinay for SQL Server SELECT LAST N Rows

To display last 3 rows without using order by: select * from Lms_Books_Details where Book_Code not in (select top((select COUNT(*) from Lms_Books_Details ) -3 ) book_code from Lms_Books_Details)

View Article


Answer by ABI for SQL Server SELECT LAST N Rows

I tested JonVD's code, but found it was very slow, 6s. This code took 0s. SELECT TOP(5) ORDERID, CUSTOMERID, OrderDate FROM Orders where EmployeeID=5 Order By OrderDate DESC

View Article

Answer by AdaTheDev for SQL Server SELECT LAST N Rows

Is "Id" indexed? If not, that's an important thing to do (I suspect it is already indexed). Also, do you need to return ALL columns? You may be able to get a substantial improvement in speed if you...

View Article


Answer by JonVD for SQL Server SELECT LAST N Rows

You can do it by using the ROW NUMBER BY PARTITION Feature also. A great example can be found here: I am using the Orders table of the Northwind database... Now let us retrieve the Last 5 orders placed...

View Article

SQL Server SELECT LAST N Rows

This is a known question but the best solution I've found is something like: SELECT TOP N * FROM MyTable ORDER BY Id DESC I've a table with lots of rows. It is not a posibility to use that query...

View Article


Answer by olafk for SQL Server SELECT LAST N Rows

MS doesn't support LIMIT in t-sql. Most of the times i just get MAX(ID) and then subtract.select * from ORDERS where ID >(select MAX(ID)-10 from ORDERS) This will return less than 10 records when ID...

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article
Browsing latest articles
Browse All 21 View Live




Latest Images