[SQL Server 2005]在记录集中增加行号
2008-07-20 08:59
197 查看
SQL Server 2005引入了ROW_NUMBER()函数。通过该函数,可以在记录集中方便的增加行号。
以下为在MSDN的解释:
ROW_NUMBER (Transact-SQL)
Updated: 17 July 2006
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
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 syntax of PARTITION BY, 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.
Return Types
bigint
Remarks
The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER within a specified partition.
Examples
Note:
The ORDER BY in the OVER clause orders ROW_NUMBER. If you add an ORDER BY clause to the SELECT statement that orders by a column(s) other than 'Row Number' the result set will be ordered by the outer ORDER BY.
The following example returns the ROW_NUMBER for the salespeople in AdventureWorks based on the year-to-date sales.
Copy Code
The following example returns rows with numbers 50 to 60 inclusive in the order of the OrderDate.
Copy Code
The following example shows using the PARTITION BY argument.
Copy Code
以下为在MSDN的解释:
ROW_NUMBER (Transact-SQL)
Updated: 17 July 2006
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> )
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 syntax of PARTITION BY, 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.
Return Types
bigint
Remarks
The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER within a specified partition.
Examples
Note:
The ORDER BY in the OVER clause orders ROW_NUMBER. If you add an ORDER BY clause to the SELECT statement that orders by a column(s) other than 'Row Number' the result set will be ordered by the outer ORDER BY.
The following example returns the ROW_NUMBER for the salespeople in AdventureWorks based on the year-to-date sales.
Copy Code
USE AdventureWorks; GO SELECT c.FirstName, c.LastName ,ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS 'Row Number' ,s.SalesYTD, a.PostalCode FROM Sales.SalesPerson s INNER JOIN Person.Contact c ON s.SalesPersonID = c.ContactID INNER JOIN Person.Address a ON a.AddressID = c.ContactID WHERE TerritoryID IS NOT NULL AND SalesYTD <> 0;
The following example returns rows with numbers 50 to 60 inclusive in the order of the OrderDate.
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;
The following example shows using the PARTITION BY argument.
Copy Code
USE AdventureWorks; GO SELECT c.FirstName, c.LastName ,ROW_NUMBER() OVER (PARTITION BY PostalCode ORDER BY SalesYTD DESC) AS 'Row Number' ,s.SalesYTD, a.PostalCode FROM Sales.SalesPerson s INNER JOIN Person.Contact c ON s.SalesPersonID = c.ContactID INNER JOIN Person.Address a ON a.AddressID = c.ContactID WHERE TerritoryID IS NOT NULL AND SalesYTD <> 0;
相关文章推荐
- SQL Server 2005删除重复记录的新方法
- MySQL中在查询结果集中得到记录行号的方法
- 如何在SQL Server 2005中为安装程序增加计数器注册表项值
- 从 SQL Server 2005 Beta 2 升级到 SQL Server 2005 社区技术预览后记录消息"无法找到指定的模块"错误和所有 SQL Server 代理作业不运行
- 本站原创sql server 2005 分页存储过程(同时得到记录总数)及其运用
- 如何手工在 SQL Server 2005 中为安装程序增加计数器注册表项值
- SQL SERVER 2005中全新的OUTPUT子句添加数据记录详解(理论篇)
- sql server 2005中新增加的try catch的用法
- SQL SERVER 2005 显示行号
- SQL Server 2005“备份集中的数据库备份与现有的数据库不同”解决方法
- 使用CET和row_number()删除重复记录 ( sql server 2005)
- SQL Server 2005“备份集中的数据库备份与现有的数据库不同”解决方法
- 在MySQL查询结果集中得到记录行号
- SQL Server 删除重复记录,适合2005以后版本
- 批量更新具有数千属性列的数据表的记录信息(SQL Server 2005)
- MySQL中在查询结果集中得到记录行号的方法
- mysql 在查询语句返回的列中包含一列表示该条记录在整个结果集中的行号
- [转]如何在 SQL Server 2005 中为安装程序增加计数器注册表项值
- SQL Server 2005 一千万条以上记录分页数据库优化经验总结(转载)
- SQL Server 2005新增加的功能