简单说明和示例:
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
为查询出来的每一行记录生成一个序号。
Syntax: ROW_NUMBER () OVER ( [ <partition_by_clause> ] <order_by_clause> )
partition 用来分组,如果有partition语句,行号将按组分配,如果没有,行号将按整个结果集分配。
order by 用来决定按什么顺序分配行号,这里的order by与select语句的order by没有关系。
使用: 数据分页, 取某一段数据, 去除重复行
eg. :
USE AdventureWorks;
GO
WITH OrderedOrders AS
(SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (order by OrderDate)as RowNumber
FROM Sales.SalesOrderHeader )
SELECT *
FROM OrderedOrders
WHERE RowNumber between 50 and 60;