当前位置:  数据库>sqlserver

SQL Server 排序函数 ROW_NUMBER和RANK 用法总结

    来源: 互联网  发布时间:2014-09-05

    本文导语:  1.ROW_NUMBER()基本用法: SELECT  SalesOrderID,  CustomerID,  ROW_NUMBER() OVER (ORDER BY SalesOrderID) AS RowNumber FROM Sales.SalesOrderHeader结果集:SalesOrderID    CustomerID    RowNumber--------------- ------------- ---------------43659           676       ...

1.ROW_NUMBER()基本用法:

SELECT
  SalesOrderID,
  CustomerID,
  ROW_NUMBER() OVER (ORDER BY SalesOrderID) AS RowNumber
 FROM Sales.SalesOrderHeader
结果集:
SalesOrderID    CustomerID    RowNumber
--------------- ------------- ---------------
43659           676           1
43660           117           2
43661           442           3
43662           227           4
43663           510           5
43664           397           6
43665           146           7
43666           511           8
43667           646           9
 :

2.RANK()基本用法:

SELECT
  SalesOrderID,
  CustomerID,
  RANK() OVER (ORDER BY CustomerID) AS Rank
 FROM Sales.SalesOrderHeader
结果集:
SalesOrderID    CustomerID    Rank
--------------- ------------- ----------------
43860           1             1
44501           1             1
45283           1             1
46042           1             1
46976           2             5
47997           2             5
49054           2             5
50216           2             5
51728           2             5
57044           2             5
63198           2             5
69488           2             5
44124           3             13
 :

3.利用CTE来过滤ROW_NUMBER()的用法:

WITH NumberedRows AS
(
  SELECT
    SalesOrderID,
    CustomerID,
    ROW_NUMBER() OVER (ORDER BY SalesOrderID) AS RowNumber
   FROM Sales.SalesOrderHeader
)

SELECT * FROM NumberedRows
 WHERE RowNumber BETWEEN 100 AND 200
结果集:

SalesOrderID    CustomerID    RowNumber
--------------- ------------- --------------
43759           13257         100
43760           16352         101
43761           16493         102
 :
43857           533           199
43858           36            200

4.带Group by的ROW_NUMBER()用法:

WITH CustomerSum
AS
(
  SELECT CustomerID, SUM(TotalDue) AS TotalAmt
   FROM Sales.SalesOrderHeader
   GROUP BY CustomerID
)
SELECT
  *,
  ROW_NUMBER() OVER (ORDER BY TotalAmt DESC) AS RowNumber
 FROM CustomerSum
结果集:
CustomerID    TotalAmt        RowNumber
------------- --------------- ---------------
678           1179857.4657    1
697           1179475.8399    2
170           1134747.4413    3
328           1084439.0265    4
514           1074154.3035    5
155           1045197.0498    6
72            1005539.7181    7
 :

5.ROW_NUMBER()或是RANK()聚合用法:

WITH CustomerSum AS
(
  SELECT CustomerID, SUM(TotalDue) AS TotalAmt
   FROM Sales.SalesOrderHeader
   GROUP BY CustomerID
)
SELECT  *,
  RANK() OVER (ORDER BY TotalAmt DESC) AS Rank
--或者是ROW_NUMBER() OVER (ORDER BY TotalAmt DESC) AS Row_Number
 FROM CustomerSum
RANK()的结果集:
CustomerID  TotalAmt              Rank
----------- --------------------- --------------------
678         1179857.4657          1
697         1179475.8399          2
170         1134747.4413          3
328         1084439.0265          4
514         1074154.3035          5
 :

6.DENSE_RANK()基本用法:

SELECT
  SalesOrderID,
  CustomerID,
  DENSE_RANK() OVER (ORDER BY CustomerID) AS DenseRank
 FROM Sales.SalesOrderHeader
 WHERE CustomerID > 100
结果集:
SalesOrderID CustomerID  DenseRank
------------ ----------- --------------------
46950        101         1
47979        101         1
49048        101         1
50200        101         1
51700        101         1
57022        101         1
63138        101         1
69400        101         1
43855        102         2
44498        102         2
45280        102         2
46038        102         2
46951        102         2
47978        102         2
49103        102         2
50199        102         2
51733        103         3
57058        103         3
 :

7.RANK()与DENSE_RANK()的比较:

WITH CustomerSum AS
(
  SELECT
    CustomerID,
    ROUND(CONVERT(int, SUM(TotalDue)) / 100, 8) * 100 AS TotalAmt
   FROM Sales.SalesOrderHeader
   GROUP BY CustomerID
)
SELECT *,
  RANK() OVER (ORDER BY TotalAmt DESC) AS Rank,
  DENSE_RANK() OVER (ORDER BY TotalAmt DESC) AS DenseRank
 FROM CustomerSum
结果集:
CustomerID  TotalAmt    Rank    DenseRank
----------- ----------- ------- --------------------
697         1272500     1       1
678         1179800     2       2
170         1134700     3       3
328         1084400     4       4
 :
87          213300      170     170
667         210600      171     171
196         207700      172     172
451         206100      173     173
672         206100      173     173
27          205200      175     174
687         205200      175     174
163         204000      177     175
102         203900      178     176
 :

8.NTILE()基本用法:

SELECT
  SalesOrderID,
  CustomerID,
  NTILE(10000) OVER (ORDER BY CustomerID) AS NTile
 FROM Sales.SalesOrderHeader
结果集:
SalesOrderID    CustomerID    NTile
--------------- ------------- ---------------
43860           1             1
44501           1             1
45283           1             1
46042           1             1
46976           2             2
47997           2             2
49054           2             2
50216           2             2
51728           2             3
57044           2             3
63198           2             3
69488           2             3
44124           3             4
 :
45024           29475         9998
45199           29476         9998
60449           29477         9998
60955           29478         9999
49617           29479         9999
62341           29480         9999
45427           29481         10000
49746           29482         10000
49665           29483         10000

9.所有排序方法对比:

SELECT
  SalesOrderID AS OrderID,
  CustomerID,
  ROW_NUMBER() OVER (ORDER BY CustomerID) AS RowNumber,
  RANK() OVER (ORDER BY CustomerID) AS Rank,
  DENSE_RANK() OVER (ORDER BY CustomerID) AS DenseRank,
  NTILE(10000) OVER (ORDER BY CustomerID) AS NTile
 FROM Sales.SalesOrderHeader
结果集:
OrderID  CustomerID    RowNumber Rank    DenseRank NTile
-------- ------------- --------- ------- --------- --------
43860    1             1         1       1         1
44501    1             2         1       1         1
45283    1             3         1       1         1
46042    1             4         1       1         1
46976    2             5         5       2         2
47997    2             6         5       2         2
49054    2             7         5       2         2
50216    2             8         5       2         2
51728    2             9         5       2         3
57044    2             10        5       2         3
63198    2             11        5       2         3
69488    2             12        5       2         3
44124    3             13        13      3         4
44791    3             14        13      3         4
 :

10.PARTITION BY基本使用方法:

SELECT
  SalesOrderID,
  SalesPersonID,
  OrderDate,
  ROW_NUMBER() OVER (PARTITION BY SalesPersonID ORDER BY OrderDate) AS OrderRank
 FROM Sales.SalesOrderHeader
 WHERE SalesPersonID IS NOT NULL
结果集:
SalesOrderID    SalesPersonID    OrderDate    OrderRank
--------------- ---------------- ------------ --------------
 :
43659           279              2001-07-01 00:00:00.000    1
43660           279              2001-07-01 00:00:00.000    2
43681           279              2001-07-01 00:00:00.000    3
43684           279              2001-07-01 00:00:00.000    4
43685           279              2001-07-01 00:00:00.000    5
43694           279              2001-07-01 00:00:00.000    6
43695           279              2001-07-01 00:00:00.000    7
43696           279              2001-07-01 00:00:00.000    8
43845           279              2001-08-01 00:00:00.000    9
43861           279              2001-08-01 00:00:00.000    10
 :
48079           287              2002-11-01 00:00:00.000    1
48064           287              2002-11-01 00:00:00.000    2
48057           287              2002-11-01 00:00:00.000    3
47998           287              2002-11-01 00:00:00.000    4
48001           287              2002-11-01 00:00:00.000    5
48014           287              2002-11-01 00:00:00.000    6
47982           287              2002-11-01 00:00:00.000    7
47992           287              2002-11-01 00:00:00.000    8
48390           287              2002-12-01 00:00:00.000    9
48308           287              2002-12-01 00:00:00.000    10
 :


11.PARTITION BY聚合使用方法:
WITH CTETerritory AS
(
  SELECT
    cr.Name AS CountryName,
    CustomerID,
    SUM(TotalDue) AS TotalAmt
   FROM
    Sales.SalesOrderHeader AS soh
    INNER JOIN Sales.SalesTerritory AS ter ON soh.TerritoryID = ter.TerritoryID
    INNER JOIN Person.CountryRegion AS cr ON cr.CountryRegionCode = ter.
CountryRegionCode
   GROUP BY
    cr.Name, CustomerID
)
SELECT
  *,
  RANK() OVER(PARTITION BY CountryName ORDER BY TotalAmt, CustomerID DESC) AS Rank
 FROM CTETerritory


结果集:

CountryName    CustomerID    TotalAmt    Rank
-------------- ------------- ----------- --------------
Australia      29083         4.409       1
Australia      29061         4.409       2
Australia      29290         5.514       3
Australia      29287         5.514       4
Australia      28924         5.514       5
 :
Canada         29267         5.514       1
Canada         29230         5.514       2
Canada         28248         5.514       3
Canada         27628         5.514       4
Canada         27414         5.514       5
 :
France         24538         4.409       1
France         24535         4.409       2
France         23623         4.409       3
France         23611         4.409       4
France         20961         4.409       5
 :

12.PARTITION BY求平均数使用方法:

WITH CTETerritory AS
(
  SELECT
    cr.Name AS CountryName,
    CustomerID,
    SUM(TotalDue) AS TotalAmt
   FROM
    Sales.SalesOrderHeader AS soh
    INNER JOIN Sales.SalesTerritory AS ter ON soh.TerritoryID = ter.TerritoryID
    INNER JOIN Person.CountryRegion AS cr ON cr.CountryRegionCode = ter.
CountryRegionCode
   GROUP BY
    cr.Name, CustomerID
)
SELECT
  *,
  RANK() OVER (PARTITION BY CountryName ORDER BY TotalAmt, CustomerID DESC) AS Rank,
  AVG(TotalAmt) OVER(PARTITION BY CountryName) AS Average
 FROM CTETerritory


结果集:

CountryName    CustomerID    TotalAmt    Rank    Average
-------------- ------------- ----------- ------- ------------------
Australia      29083         4.409       1       3364.8318
Australia      29061         4.409       2       3364.8318
Australia      29290         5.514       3       3364.8318
 :
Canada         29267         5.514       1       12824.756
Canada         29230         5.514       2       12824.756
Canada         28248         5.514       3       12824.756


    
 
 

您可能感兴趣的文章:

  • mysql的SQL_NO_CACHE(在查询时不使用缓存)和sql_cache用法
  • C#调用SQL语句时乘号的用法
  • sql中all,any,some用法
  • SQL事务用法begin tran,commit tran和rollback tran的用法
  • sql游标的用法简单笔记
  • sql查询语句group by用法简介
  • sql server中Exists的用法举例
  • 浅析SQL语句中GROUP BY的用法
  • sql多表级联更新update的用法举例
  • sql server中ISNULL函数与Mysql中IFNULL函数的用法
  • SQL中object_id函数的用法
  • SQL cursor用法实例
  • sql语句中isnull函数用法 实例教程
  • sql Set IDENTITY_INSERT的用法
  • if exists和if not exists关键字用法(sql server)
  • SQL SELECT DISTINCT 语句用法
  • sql数据库中ISNULL函数用法 sql语句判断值是否为空
  • [Oracle] 常用工具集之SQL*Loader的用法
  • HTML标签参考手册 iis7站长之家
  • sql server中Group By语句的用法
  • sql server 存储过程中If Else的用法举例
  • SQL学习笔记八 索引,表连接,子查询,ROW_NUMBER
  • sql Server使用ROW_NUMBER时不排序的实例代码
  • sql server 2005中row_number实现分页的例子
  • sql server分组排序之row_number()over函数的使用
  • ROW_NUMBER SQL Server 2005的LIMIT功能实现(ROW_NUMBER()排序函数)
  • Sql server中Row_Number函数详解
  • SQL中Group分组获取Top N方法实现可首选row_number
  • C#拼接SQL语句 用ROW_NUMBER实现的高效分页排序
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 在SQL中有函数,比如COUNT(*),怎样通过JDBC调用获得函数值????
  • SQL中含函数,比如COUNT(*),怎样在JDBC接口中取出函数值????
  • sql数据库不能直接用instr函数
  • T-SQL常用的聚合函数
  • sql server中自定义一个简单的 split 函数
  • 初学者问题:标准的sql里有处理日期的函数吗?
  • access与sql server几个常用函数的区别(图文)
  • 分享:sql server 取日期的年月日部分的函数
  • sql函数实现递归查询示例
  • T-SQL常用的数学函数
  • T-SQL实例 函数结果设置为列别名
  • SQL Server中的数据复制到的Access中的函数
  • C处理SQL命令字符串的函数哪里有?
  • SQL中函数 replace 的参数1的数据类型ntext无效的解决方法
  • SQL中有count这个函数,请问如何将这个值取出
  • 高分求java.sql类库的类说明,函数及方法!
  • php is_numberic函数产生sql注入漏洞怎么解决
  • sql函数CAST与CONVERT的区别
  • SQL的SUBSTR()函数使用介绍
  • sql2005 HashBytes 加密函数
  • java命名空间java.sql接口statement的类成员方法: executeupdate定义及介绍
  • 请问,这是什么错误!java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][Named Pipes]??????? SQL Server?虽然分少,但一定给,只要您是前5名回复者中最好的以为!
  • java命名空间java.sql接口connection的类成员方法: nativesql定义及介绍
  • SQL查询分析工具 SQL Workbench/J
  • java命名空间java.sql接口preparedstatement的类成员方法: executeupdate定义及介绍
  • oracle导出sql语句的结果集和保存执行的sql语句(深入分析)
  • java命名空间java.sql接口rowid的类成员方法: getbytes定义及介绍
  • SQL Server统计SQL语句执行时间的脚本
  • java命名空间java.sql接口ref的类成员方法: getbasetypename定义及介绍
  • SQL客户端软件 PKLite SQL Client
  • java命名空间java.sql接口databasemetadata的类成员方法: getsqlkeywords定义及介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3