SQL Server SELECT LAST N Rows

前端 未结 18 2131
猫巷女王i
猫巷女王i 2020-11-28 03:13

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 wi

相关标签:
18条回答
  • 2020-11-28 04:05
    select * from (select top 6 * from vwTable order by Hours desc) T order by Hours
    
    0 讨论(0)
  • 2020-11-28 04:05

    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; I don't know if this applies to Sybase/MS SQL Server.

    0 讨论(0)
  • 2020-11-28 04:06

    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
    
    0 讨论(0)
  • 2020-11-28 04:06

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

    0 讨论(0)
  • 2020-11-28 04:09

    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 but differrent ways. thank you guys.

     select * from Products except select top (77-10) * from Products
    

    in this way you can get last 10 rows but order will show descnding way

    select top 10 * from products
     order by productId desc 
    

     select * from products
     where productid in (select top 10 productID from products)
     order by productID desc
    

     select * from products where productID not in 
     (select top((select COUNT(*) from products ) -10 )productID from products)
    
    0 讨论(0)
  • 2020-11-28 04:09

    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 real world applications, for example I do this for non-historic Recent Weather Data, or recent News feed searches or Recent GPS location data point data.

    This is a huge performance improvement if you know for certain that your rows are in the most recent TOP 5% of the table for example. Such that even if there are indexes on the Tables, it further limits the possibilites to only 5% of rows in tables which have 100+ million or 1+ billion rows. This is especially the case when Older Data will require Physical Disk reads and not only Logical In Memory reads.

    This is well more efficient than SELECT TOP | PERCENT | LIMIT as it does not select the rows, but merely limit the portion of the data to be searched.

    DECLARE @RowIdTableA BIGINT
    DECLARE @RowIdTableB BIGINT
    DECLARE @TopPercent FLOAT
    
    -- Given that there is an Sequential Identity Column
    -- Limit query to only rows in the most recent TOP 5% of rows
    SET @TopPercent = .05
    SELECT @RowIdTableA = (MAX(TableAId) - (MAX(TableAId) * @TopPercent)) FROM TableA
    SELECT @RowIdTableB = (MAX(TableBId) - (MAX(TableBId) * @TopPercent)) FROM TableB
    
    SELECT *
    FROM TableA a
    INNER JOIN TableB b ON a.KeyId = b.KeyId
    WHERE a.Id > @RowIdTableA AND b.Id > @RowIdTableB AND
          a.SomeOtherCriteria = 'Whatever'
    
    0 讨论(0)
提交回复
热议问题