hive over函式應用

2021-07-31 18:00:23 字數 3466 閱讀 6901

over:視窗函式

建表及插入資料:

create table temp.over_test (

name string,

part string,

salary decimal(30,8),

age string

)

row format delimited fields terminated by '\036'

stored as rcfile;

insert into table temp.over_test select '11','a','100','12' ;

insert into table temp.over_test select '21','a','211','12' ;

insert into table temp.over_test select '11','a','323','34' ;

insert into table temp.over_test select '241','b','432','12' ;

insert into table temp.over_test select '231','b','123','12' ;

insert into table temp.over_test select '2121','a','422','12' ;

insert into table temp.over_test select '2012','a','422','12' ;

insert into table temp.over_test select '13','c','131','12' ;

insert into table temp.over_test select '123','b','34','12' ;

insert into table temp.over_test select '342','e','789','12' ;

insert into table temp.over_test select '1222231','s','555','12' ;

insert into table temp.over_test select '675','e','666','12' ;

insert into table temp.over_test select '51','b','33','12' ;

insert into table temp.over_test select '41','b','222','12' ;

insert into table temp.over_test select '11','a','36','12' ;

應用:

select * from temp.over_test

select name,salary,max(salary)over(partition by part) from temp.over_test

select name,part,salary, sum(salary)over(order by salary range between 20 preceding and 20 following) mm

from temp.over_test--salary +-20範圍內salary相加

select name,part,salary, sum(salary)over(order by salary rows between 1 preceding and 2 following) mm

from temp.over_test--前1後2行一起相加

select name,part,salary, rank()over(partition by part order by salary desc) from temp.over_test--跳號排序1、1、3

select name,part,salary, row_number()over(partition by part order by salary desc) from temp.over_test--排序加唯一行號

select name,part,salary, dense_rank()over(partition by part order by salary desc) from temp.over_test--不跳號排序1、1、2

select name,part,salary, sum(salary)over(partition by part order by salary) from temp.over_test--本次之前salary相加

select name,part,salary, lag(cast(salary as string),1,'')over(order by salary) as qian from temp.over_test--排序後前一行值,無用''代替(string 型別下有效)

select name,part,salary, lead(cast(salary as string),1,'')over(partition by part order by salary desc) as qian--後一行資料

from temp.over_test

select name,part,salary, ntile(4)over(order by salary desc) as qian from temp.over_test--資料四分片返回所在分偏值

select salary,ratio_to_report(salary)over() from temp.over_test --didn't support 求salary的百分比

select name,part,salary,percent_rank()over(partition by part order by name desc) from temp.over_test--(所在行-1)/(分類後總行-1)

select name,part,salary,cume_dist()over(partition by part order by salary desc) from temp.over_test--[所在行-1(並列則+並列數-1)]/總行數

select salary,

percentile_cont(0.7) within group(order by salary) over(partition by part) "percentile_cont",

percent_rank()over(partition by part order by name desc) from temp.over_test

函式應用AS Atan2函式應用

package public function init public function djhs e 方法 每日一道理 乙個安靜的夜晚,我獨自一人,有些空虛,有些淒涼。坐在星空下,抬頭仰望美麗天空,感覺真實卻由虛幻,閃閃爍爍,似乎看來還有些跳動。美的一切總在瞬間,如同 海市蜃樓 般,也只是剎那間的...

va start va end 函式應用

1 當無法列出傳遞函式的所有實參的型別和數目時,可用省略號指定參數列 void foo void foo parm list,2 函式引數的傳遞原理 函式引數是以資料結構 棧的形式訪問,從右至左入棧.eg include void fun int a,int main output 1 2 3 4 ...

Mysql 函式應用

摘自 daisylh 部落格 1.用if.then.elseif.delimiter drop function if exists demodb sf1 create function sf1 pid varchar 10 returns varchar 100 begin declare str...