<SPAN style="COLOR: #000000">ROW_NUMBER()说明:返回结果集分区内行的序列号,每个分区的第一行从 <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1<SPAN style="COLOR: #000000"> 开始。 语法:ROW_NUMBER () <SPAN style="COLOR: #0000ff">OVER<SPAN style="COLOR: #000000"> ( <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000"> <SPAN style="COLOR: #ff0000">]<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000">order_by_clause<SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000"> ) 。 备注:<SPAN style="COLOR: #0000ff">ORDER<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">BY<SPAN style="COLOR: #000000"> 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。 参数:<SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000">partition_by_clause<SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000"> :将 <SPAN style="COLOR: #0000ff">FROM<SPAN style="COLOR: #000000"> 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。 <SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000">order_by_clause<SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000">:确定将 ROW_NUMBER 值分配给分区中的行的顺序。 返回类型:<SPAN style="COLOR: #0000ff">bigint<SPAN style="COLOR: #000000"> 。示例: <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">以下示例将根据年初至今的销售额,返回 AdventureWorks 中销售人员的 ROW_NUMBER。<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #000000"><SPAN style="COLOR: #0000ff">USE<SPAN style="COLOR: #000000"> AdventureWorks <SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">SELECT<SPAN style="COLOR: #000000"> c.FirstName,c.LastName,ROW_NUMBER() <SPAN style="COLOR: #0000ff">OVER<SPAN style="COLOR: #000000">(<SPAN style="COLOR: #0000ff">ORDER<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">BY<SPAN style="COLOR: #000000"> SalesYTD <SPAN style="COLOR: #0000ff">DESC<SPAN style="COLOR: #000000">) <SPAN style="COLOR: #0000ff">AS<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #ff0000">Row Number<SPAN style="COLOR: #ff0000">'<SPAN style="COLOR: #000000">,s.SalesYTD,a.PostalCode <SPAN style="COLOR: #0000ff">FROM<SPAN style="COLOR: #000000"> Sales.SalesPerson s <SPAN style="COLOR: #808080">JOIN<SPAN style="COLOR: #000000"> Person.Contact c <SPAN style="COLOR: #0000ff">on<SPAN style="COLOR: #000000"> s.SalesPersonID <SPAN style="COLOR: #808080">=<SPAN style="COLOR: #000000"> c.ContactID <SPAN style="COLOR: #808080">JOIN<SPAN style="COLOR: #000000"> Person.Address a <SPAN style="COLOR: #0000ff">ON<SPAN style="COLOR: #000000"> a.AddressID <SPAN style="COLOR: #808080">=<SPAN style="COLOR: #000000"> c.ContactID <SPAN style="COLOR: #0000ff">WHERE<SPAN style="COLOR: #000000"> TerritoryID <SPAN style="COLOR: #0000ff">IS<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080">NOT<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">NULL<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080">AND<SPAN style="COLOR: #000000"> SalesYTD <SPAN style="COLOR: #808080"><><SPAN style="COLOR: #000000"> <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080"> FirstName LastName Row Number SalesYTD PostalCode --------- ---------- ---------- ------------ ---------------------------- Shelley Dyck 1 5200475.2313 98027 Gail Erickson 2 5015682.3752 98055 Maciej Dusza 3 4557045.0459 98027 Linda Ecoffey 4 3857163.6332 98027 Mark Erickson 5 3827950.238 98055 Terry Eminhizer 6 3587378.4257 98055 Michael Emanuel 7 3189356.2465 98055 Jauna Elson 8 3018725.4858 98055 Carol Elliott 9 2811012.7151 98027 Janeth Esteves 10 2241204.0424 98055 Martha Espinoza 11 1931620.1835 98055 Carla Eldridge 12 1764938.9859 98027 Twanna Evans 13 1758385.926 98055 (13 行受影响) <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #000000"><SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">以下示例将返回行号为 50 到 60(含)的行,并以 OrderDate 排序。<SPAN style="COLOR: #008080">/<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">USE<SPAN style="COLOR: #000000"> AdventureWorks; <SPAN style="COLOR: #0000ff">GO<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">WITH<SPAN style="COLOR: #000000"> OrderedOrders <SPAN style="COLOR: #0000ff">AS<SPAN style="COLOR: #000000"> (<SPAN style="COLOR: #0000ff">SELECT<SPAN style="COLOR: #000000"> SalesOrderID,OrderDate, ROW_NUMBER() <SPAN style="COLOR: #0000ff">OVER<SPAN style="COLOR: #000000"> (<SPAN style="COLOR: #0000ff">order<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">by<SPAN style="COLOR: #000000"> OrderDate)<SPAN style="COLOR: #0000ff">as<SPAN style="COLOR: #000000"> RowNumber <SPAN style="COLOR: #0000ff">FROM<SPAN style="COLOR: #000000"> Sales.SalesOrderHeader ) <SPAN style="COLOR: #0000ff">SELECT<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080"><SPAN style="COLOR: #000000"> <SPAN style="COLOR: #0000ff">FROM<SPAN style="COLOR: #000000"> OrderedOrders <SPAN style="COLOR: #0000ff">WHERE<SPAN style="COLOR: #000000"> RowNumber <SPAN style="COLOR: #808080">between<SPAN style="COLOR: #000000"> <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">50<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080">and<SPAN style="COLOR: #000000"> <SPAN style="FONT-WEIGHT: bold; COLOR: #800000">60<SPAN style="COLOR: #000000">; <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080"> SalesOrderID OrderDate RowNumber ------------ ----------------------- -------------------- 43708 2001-07-03 00:00:00.000 50 43709 2001-07-03 00:00:00.000 51 43710 2001-07-03 00:00:00.000 52 43711 2001-07-04 00:00:00.000 53 43712 2001-07-04 00:00:00.000 54 43713 2001-07-05 00:00:00.000 55 43714 2001-07-05 00:00:00.000 56 43715 2001-07-05 00:00:00.000 57 43716 2001-07-05 00:00:00.000 58 43717 2001-07-05 00:00:00.000 59 43718 2001-07-06 00:00:00.000 60 (11 行受影响) <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #000000"><SPAN style="COLOR: #008080">--<SPAN style="COLOR: #008080">------------------------------------------------------------<SPAN style="COLOR: #008080"> <SPAN style="COLOR: #000000">RANK()说明:返回结果集的分区内每行的排名。行的排名是相关行之前的排名数加一。 语法:RANK () <SPAN style="COLOR: #0000ff">OVER<SPAN style="COLOR: #000000"> ( <SPAN style="COLOR: #ff0000">[<SPAN style="COLOR: #ff0000"> < partition_by_clause > <SPAN style="COLOR: #ff0000">]<SPAN style="COLOR: #000000"> <SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000"> order_by_clause <SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000"> ) 备注:如果两个或多个行与一个排名关联,则每个关联行将得到相同的排名。 例如,如果两位顶尖销售员具有同样的 SalesYTD 值,他们将并列第一。 由于已有两行排名在前,所以具有下一个最大 SalesYTD 的销售人员将排名第三。 因此,RANK 函数并不总返回连续整数。 用于整个查询的排序顺序决定了行在结果集中的显示顺序。这也隐含了行在每个分区中的排名。 参数:<SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000"> partition_by_clause <SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000"> :将 <SPAN style="COLOR: #0000ff">FROM<SPAN style="COLOR: #000000"> 子句生成的结果集划分为要应用 RANK 函数的分区。 <SPAN style="COLOR: #808080"><<SPAN style="COLOR: #000000"> order_by_clause <SPAN style="COLOR: #808080">><SPAN style="COLOR: #000000">:确定将 RANK 值应用于分区中的行时所基于的顺序。 返回类型:<SPAN style="COLOR: #0000ff">bigint<SPAN style="COLOR: #000000">示例: <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #008080">以下示例按照数量对清单中的产品进行了排名。行集按 LocationID 分区,按 Quantity 排序。 USE AdventureWorks; GO SELECT i.ProductID,p.Name,i.LocationID,i.Quantity,RANK() OVER (PARTITION BY i.LocationID order by i.Quantity) as RANK FROM Production.ProductInventory i JOIN Production.Product p ON i.ProductID = p.ProductID ORDER BY p.Name GO / ProductID Name LocationID Quantity RANK ----------- -------------------------------------------------- ---------- -------- -------------------- 1 Adjustable Race 6 324 71 1 Adjustable Race 1 408 78 1 Adjustable Race 50 353 117 2 Bearing Ball 6 318 67 2 Bearing Ball 1 427 85 2 Bearing Ball 50 364 122 3 BB Ball Bearing 50 324 106 3 BB Ball Bearing 1 585 110 3 BB Ball Bearing 6 443 115 4 Headset Ball Bearings 1 512 99 4 Headset Ball Bearings 6 422 108 4 Headset Ball Bearings 50 388 140 316 Blade 10 388 33 ...... (1069 行受影响) <SPAN style="COLOR: #008080">/<SPAN style="COLOR: #000000">
SQL code
--语法:DENSE_RANK () 备注:如果有两个或多个行受同一个分区中排名的约束,则每个约束行将接收相同的排名。 例如,如果两位顶尖销售员具有相同的 SalesYTD 值,则他们将并列第一。 接下来 SalesYTD 最高的销售人员排名第二。该排名等于该行之前的所有行数加一。 因此,DENSE_RANK 函数返回的数字没有间断,并且始终具有连续的排名。 整个查询所用的排序顺序确定了各行在结果中的显示顺序。这说明排名第一的行可以不是分区中的第一行。 参数: 返回类型:ProductID Name LocationID Quantity DENSE_RANK ----------- -------------------------------------------------- ---------- -------- -------------------- 1 Adjustable Race 1 408 57 1 Adjustable Race 6 324 52 1 Adjustable Race 50 353 82 879 All-Purpose Bike Stand 7 144 34 712 AWC Logo Cap 7 288 38 3 BB Ball Bearing 50 324 74 3 BB Ball Bearing 6 443 81 3 BB Ball Bearing 1 585 82 ROW_NUMBER() RANK() DENSE_RANK() orderid qty rownumber rank denserank ----------- ----------- -------------------- -------------------- -------------------- 30001 10 1 1 1 10001 10 2 1 1 10006 10 3 1 1 40005 10 4 1 1 30003 15 5 5 2 30004 20 6 6 3 20002 20 7 6 3 20001 20 8 6 3 10005 30 9 9 4 30007 30 10 9 4 30007 30 11 9 4 40001 40 12 12 5 (12 行受影响) ROW_NUMBER() RANK() DENSE_RANK() orderid qty rownumber rank denserank ----------- ----------- -------------------- -------------------- -------------------- 10001 10 1 1 1 10006 10 2 2 2 30001 10 3 3 3 40005 10 4 4 4 30003 15 5 5 5 20001 20 6 6 6 20002 20 7 7 7 30004 20 8 8 8 10005 30 9 9 9 30007 30 10 10 10 30007 30 11 10 10 40001 40 12 12 11 (12 行受影响)
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|