SQL 2005 中 ROW_NUMBER 用法
2009-08-05 14:59
393 查看
原文地址: http://msdn.microsoft.com/en-us/library/ms186734.aspx
ROW_NUMBER (Transact-SQL)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
![](http://i.msdn.microsoft.com/ms186734.05b1d166-d807-482c-891f-30b3b6b58046(en-us,SQL.100).gif)
Transact-SQL Syntax Conventions
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Syntax
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Arguments
<partition_by_clause>
Divides the result set produced by the FROM clause into partitions to which the ROW_NUMBER function is applied. For the PARTITION BY syntax, see OVER Clause (Transact-SQL).
<order_by_clause>
Determines the order in which the ROW_NUMBER value is assigned to the rows in a partition. For more information, see ORDER BY Clause (Transact-SQL). An integer cannot represent a column when the <order_by_clause> is used in a ranking function.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Return Types
bigint
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Remarks
The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER within a specified partition.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Examples
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
See Also
DENSE_RANK (Transact-SQL)
NTILE (Transact-SQL)
Ranking Functions (Transact-SQL)
Functions (Transact-SQL)
ROW_NUMBER (Transact-SQL)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
![](http://i.msdn.microsoft.com/ms186734.05b1d166-d807-482c-891f-30b3b6b58046(en-us,SQL.100).gif)
Transact-SQL Syntax Conventions
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Syntax
ROW_NUMBER ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> )
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Arguments
<partition_by_clause>
Divides the result set produced by the FROM clause into partitions to which the ROW_NUMBER function is applied. For the PARTITION BY syntax, see OVER Clause (Transact-SQL).
<order_by_clause>
Determines the order in which the ROW_NUMBER value is assigned to the rows in a partition. For more information, see ORDER BY Clause (Transact-SQL). An integer cannot represent a column when the <order_by_clause> is used in a ranking function.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Return Types
bigint
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Remarks
The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER within a specified partition.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Examples
A. Returning the row number for salespeople
The following example returns theROW_NUMBERfor the salespeople in
AdventureWorksbased on the year-to-date sales.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
SELECT FirstName, LastName, ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS 'Row Number', SalesYTD, PostalCode FROM Sales.vSalesPerson WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0;
B. Returning a subset of rows
The following example returns rows with numbers50to
60inclusive in the order of the
OrderDate.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
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;
C. Using ROW_NUMBER() with PARTITION
The following example shows using theROW_NUMBERfunction with the
PARTITION BYargument.
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Copy Code
SELECT FirstName, LastName, ROW_NUMBER() OVER(PARTITION BY PostalCode ORDER BY SalesYTD DESC) AS 'Row Number', SalesYTD, PostalCode FROM Sales.vSalesPerson WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0;
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
See Also
Reference
RANK (Transact-SQL)DENSE_RANK (Transact-SQL)
NTILE (Transact-SQL)
Ranking Functions (Transact-SQL)
Functions (Transact-SQL)
相关文章推荐
- SQL2005中row_number( )、rank( )、dense_rank( )、ntile( )函数的用法
- ROW_NUMBER用法(SQL 2005新功能)
- SQL2005中row_number( )、rank( )、dense_rank( )、ntile( )函数的用法(2)
- ROW_NUMBER用法(SQL 2005新功能)
- SQL2005中row_number( )、rank( )、dense_rank( )、ntile( )函数的用法
- row_number( )、rank( )、dense_rank( )、ntile( )函数的用法 (SQL2005以上)
- SQL Server 2005 中 ROW_NUMBER()的用法
- SQL2005 四个排名函数(row_number、rank、dense_rank和ntile)的比较
- SQL 2005 ROW_NUMBER() 语句分页 | SQL效率最高的分页查询数据
- SQL中distinct和row_number() over() 的区别及用法
- 教你学会Sql中 ROW_NUMBER的用法
- SQL2005新函数ROW_NUMBER()实现分页
- sql中函数over()和row_number()的用法
- SQL中distinct 和 row_number() over() 的区别及用法
- 用SQL 2005的ROW_NUMBER() 实现分页功能
- SQL2005 ROW_NUMBER
- SQL2005 四个排名函数(row_number、rank、dense_rank和ntile)的比较
- SQL 2005 ROW_NUMBER() 语句分页
- SQL 2005的ROW_NUMBER()实现分页功能
- MS SQL 2005 四个排序函数ROW_NUMBER、RANK、DENSE_RANK 和 NTILE简介用法/结果排名排序