设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 536|回复: 0

T-SQL RANK() , DENSE_RANK() , NTILE(), ROW_NUMBER()

[复制链接]
发表于 2011-8-24 09:35:51 | 显示全部楼层 |阅读模式
Rank():
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Usage: RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )  


Dense_Rank() :
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
Usage: DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )  


Ntile():
Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.
Usage: NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > )  


Row_Number():
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )  
Example  
All Recordset :   
  1. select * from finals
复制代码


RecordIDNameSurnameCoursePoint
1BugraPostaciMathematics89
2BugraPostaciLecture90
3DanyLoweMathematics75
4DanyLoweLecture85
5AliceMarcelMathematics77
6AliceMarcelLecture100
7SimonDuruMathematics45
8SimonDuruLecture58

  1. select row_number() over(order by RecordID desc) as RowNumber ,* from finals
复制代码



RowNumberRecordIDNameSurnameCoursePoint
18SimonDuruLecture58
27SimonDuruMathematics45
36AliceMarcelLecture100
45AliceMarcelMathematics77
54DanyLoweLecture85
63DanyLoweMathematics75
72BugraPostaciLecture90
81BugraPostaciMathematics89

  1. select ntile(2) over(order by RecordID desc) as [Ntile] ,* from finals
复制代码



NtileRecordIDNameSurnameCoursePoint
18SimonDuruLecture58
17SimonDuruMathematics45
16AliceMarcelLecture100
15AliceMarcelMathematics77
24DanyLoweLecture85
23DanyLoweMathematics75
22BugraPostaciLecture90
21BugraPostaciMathematics89

as you see ntile function just divide the scope with given number
  1. select rank() over (order by Surname) as [Rank], * from finals
复制代码

RankRecordIDNameSurnameCoursePoint
17SimonDuruMathematics45
18SimonDuruLecture58
33DanyLoweMathematics75
34DanyLoweLecture85
55AliceMarcelMathematics77
56AliceMarcelLecture100
71BugraPostaciMathematics89
72BugraPostaciLecture90

rank function is working like  match one ”Duru” as 1 match another “Duru” as 1 but total count as 2 , next surname matches “Lowe” as count +1 as 3 , another “Lowe” as 3  ; now total count is 4 matching next surname as “Marcel” is count + 1 as 5 and goes on ….

  1. select dense_rank() over (order by Surname) as [DenseRank], * from finals
复制代码



DenseRankRecordIDNameSurnameCoursePoint
17SimonDuruMathematics45
18SimonDuruLecture58
23DanyLoweMathematics75
24DanyLoweLecture85
35AliceMarcelMathematics77
36AliceMarcelLecture100
41BugraPostaciMathematics89
42BugraPostaciLecture90
Dense rank its obvious like shown.

if you want to make your denserank number reseting by groups there is a way use PARTITION BY
below example reseting ranks by using “partition by” by course column

  1. select dense_rank() over (Partition by course order by Surname) as [DenseRank], * from finals  
复制代码


DenseRankRecordIDNameSurnameCoursePoint
18SimonDuruLecture58
24DanyLoweLecture85
36AliceMarcelLecture100
42BugraPostaciLecture90
17SimonDuruMathematics45
23DanyLoweMathematics75
35AliceMarcelMathematics77
41BugraPostaciMathematics89
Thats all folks…

http://blog.bugrapostaci.com/2010/01/26/t-sql-rank-dense_rank-ntile-row_number/


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2026-2-5 13:18 , Processed in 0.012396 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表