SQL Server ROW_NUMBER() on SQL Server 2000?

后端 未结 4 1875
终归单人心
终归单人心 2020-12-03 15:38

I have a query that allows me to get records from a database table by giving it a minimum and maximum limit.

It goes like this:

  SELECT T1.CDUSUARIO         


        
4条回答
  •  臣服心动
    2020-12-03 15:51

    I know this thread is bit old, but for anyone else looking for same solution, I think it will be useful to know that there is a good solution for this problem.

    Please see the original link here

    For those who do not want to click on the link, I have copied and pasted the code below. Again, credit goes to original publisher

    Here is the below SQL for SQL Server 2000 to select the latest version of a record grouping by a single column.

    SELECT *
      FROM (
        SELECT *, (
          SELECT COUNT(*)
            FROM MyTable AS counter
          WHERE counter.PartitionByColumn = MyTable.PartitionByColumn
            AND  counter.OrderByColumn >= MyTable.OrderByColumn
          ) AS rowNumber
        FROM MyTable
      ) AS r1
      WHERE r1.rowNumber = 1
    

    Same code in SQL Server 2005 would look like this:

    SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY PartitionByColumn 
        ORDER BY OrderByColumn DESC) AS rowNumber FROM MyTable) AS rw1 
      WHERE rw1.rowNumber = 1
    

提交回复
热议问题