ntile,PERCENT_RANK,cume_dist分析函数
分析函数:
ntile
功能描述:将一个组分为"表达式"的散列表示,例如,如果表达式=4,则给组中的每一行分配一个数(从1到4),如果组中有20行,则给前5行分配1,给下5行分配2等等。如果组的基数不能由表达式值平均分开,则对这些行进行分配时,组中就没有任何percentile的行数比其它percentile的行数超过一行,最低的percentile是那些拥有额外行的percentile。例如,若表达式=4,行数=21,则percentile=1的有6行,percentile=2的有5行等等。
sql@kokooa>select id,value,ntile(4) over (order by value) as
2 quartile from test017;
ID VALUE QUARTILE
---------- ---------- ----------
1 123 1
3 345 1
4 456 2
5 567 3
6 567 4
PERCENT_RANK
功能描述:和CUME_DIST(累积分配)函数类似,对于一个组中给定的行来说,在计算那行的序号时,先减1,然后除以n-1(n为组中所有的行数)。该函数总是返回0~1(包括1)之间的数。RANK函数对于等值的返回序列值是一样的
sql@kokooa>select id,value,percent_rank()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
1 123 0
3 345 .25
4 456 .5
5 567 .75
6 567 1
如果有重复元素呢?
sql@kokooa>select id,value,percent_rank()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
1 123 0
1 234 0
3 345 .4
4 456 .6
5 567 .8
6 567 1
其中开头两行id重复。
sql@kokooa>select id,value,percent_rank()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
1 123 0
2 234 .2
3 345 .4
3 456 .4
5 567 .8
6 567 1
sql@kokooa>select id,value,percent_rank()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
3 123 0
3 234 0
3 345 0
3 456 0
3 567 0
6 567 1
继续测:
sql@kokooa>select id,value,percent_rank()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
3 123 0
3 234 0
3 345 0
3 456 0
5 567 .8
6 567 1
应该是(5-1)/5=0.8 (6-1)/5=1;
Cume_dist
sql@kokooa>select id,value,cume_dist()over(order by id) as pr from test017;
ID VALUE PR
---------- ---------- ----------
3 123 .5
3 234 .5
3 345 .5
5 456 .833333333
5 567 .833333333
6 567 1
应该是行数/总行数。如果有重复的,如上:则前3行是3/6 接着2行是5/6。被除数以重复行的最后一行的行数为准。
|