欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-12 15:45:52
1.row_number()基本用法: select  salesorderid,  customerid,  row_number() o...

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