orace評級函式

2021-08-31 02:06:37 字數 4407 閱讀 7106

cume_dist():

功能描述:計算一行在組中的相對位置,返回大於0 小於等於1的數,例如在乙個4行組中,分布是1/4 ,2/4 ,3/4  ,4/4

如果有重複的就乘以重複的個數,如第一行和第二行重複 分布為 (1/4)*2,(1/4)*2, 3/4 ,4/4

下面是例子:

表 all_sales

select 

* from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

****************************************=

1 2003 1 1 21 10034.84

2 2003 1 2 21 1034.84

3 2003 1 3 21 1034.84

4 2003 1 4 21 3034.84

執行sql:

select

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount asc) as cume_dist

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

******************************==

1 2 1034.84 0.5

2 3 1034.84 0.5

3 4 3034.84 0.75

4 1 10034.84 1

percent_rank()是某個值相對於一組值的百分比排名,大於等於0 小於等於1

select 

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount asc) as cume_dist

,percent_rank() over(order by t.amount desc )as percent_rank

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

******************************===

1 1 10034.84 1 0

2 4 3034.84 0.75 0.333333333333333

3 2 1034.84 0.5 0.666666666666667

4 3 1034.84 0.5 0.666666666666667

如果重複的話則記錄的是小值,但是 cume_dist()記錄的是大值。

ntilte()函式記錄n分片的值。

select 

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount asc) as cume_dist

,percent_rank() over(order by t.amount asc )as percent_rank

,ntile(5) over(order by t.amount desc )as ntile

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

****************************************=

1 1 10034.84 1 1 1

2 4 3034.84 0.75 0.666666666666667 2

3 2 1034.84 0.5 0 3

4 3 1034.84 0.5 0 4

select 

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount asc) as cume_dist

,percent_rank() over(order by t.amount asc )as percent_rank

,ntile(3) over(order by t.amount desc )as ntile

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

******************************

1 1 10034.84 1 1 1

2 4 3034.84 0.75 0.666667 1

3 2 1034.84 0.5 0 2

4 3 1034.84 0.5 0 3

感覺是排名函式 並且固定了排名的總個數。

row_number():

從1開始為每個分組返回乙個數字

select 

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount asc) as cume_dist

,percent_rank() over(order by t.amount asc )as percent_rank

,ntile(2) over(order by t.amount desc )as ntile

,row_number() over(order by t.amount desc) as row_number

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

*************************====

1 1 10034.84 1 1 1 1

2 4 3034.84 0.75 0.666667 1 2

3 2 1034.84 0.5 0 2 3

4 3 1034.84 0.5 0 2 4

percentile_disc(x)

和percentile_cont(x)

的作用與cume_dist()

和percent_rank()

相反:

select 

percentile_disc(0.25) within group(order by t.amount desc)

as percentile_disc

,percentile_cont(0.666666666666667) within group(order by t.amount desc)

as percentile_cont

from all_sales t

where year =2003

and t.month =1

and t.amount is not null

and t.emp_id=21

******************************====

1 10034.84 1034.84

與下面的sql正好相反:

select 

t.prd_type_id

,t.amount

,cume_dist() over (order by t.amount desc) as cume_dist

,percent_rank() over(order by t.amount desc )as percent_rank

from all_sales t

where t.year=2003

and t.month =1

and t.amount is not null

and t.emp_id=21

********************==

1 1 10034.84 0.25 0 ***x u

2 4 3034.84 0.5 0.333333333333333 ***x u

3 3 1034.84 1 0.666666666666667 ***x u

4 2 1034.84 1 0.666666666666667 ***x u

orace後台執行

7.19 資料庫後台執行命令和客戶端登陸 sqlplus username password host port sid 普通使用者 sqlplus as sysdba或者conn as sysdba sys使用者 pl sql普通使用者登陸 database 10.171.37.11 1526 t...

orace遞迴查詢

查詢結果自己所有的前代節點 包括自己 select from orgtable o where nvl o.canceled,0 1 start with o.id 5661 connect by to char o.id prior to char o.supsubcomid 查詢結果自己所有的後...

orace時間往前推

oracle中獲取當前系統時間往前推7天的日期 2014 01 07 11 03 19 select sysdate interval 7 day from dual select to date to char sysdate,yyyy mm dd hh mi ss yyyy mm dd hh m...