over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数 over(partition by deptno)按照部门分区 over(partition by deptno order by salary)
开窗的窗口范围
over(order by salary range between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。
1 2 3 4 5 6 7 8 9 10 11 12
--sum(s)over(order by s range between 2 preceding and 2 following) 表示加2或2的范围内的求和 selectname,class,s,sum(s)over(orderby s rangebetween2precedingand2following) mm from t2 adf 34545--45加2减2即43到47,但是s在这个范围内只有45 asdf 35555 cfe 27474 3dd 378158--78在76到80范围内有78,80,求和得158 fda 180158 gds 29292 ffd 195190 dss 195190 ddd 399198 gf 399198
over(order by salary rows between 5 preceding and 5 following) :窗口范围为当前行前后各移动5行。
1 2 3 4 5 6 7 8 9 10 11 12
--sum(s)over(order by s rows between 2 preceding and 2 following)表示在上下两行之间的范围内 selectname,class,s, sum(s)over(orderby s rowsbetween2precedingand2following) mm from t2 adf 345174 (45+55+74=174) asdf 355252 (45+55+74+78=252) cfe 274332 (74+55+45+78+80=332) 3dd 378379 (78+74+55+80+92=379) fda 180419 gds 292440 ffd 195461 dss 195480 ddd 399388 gf 399293
over(order by salary range between unbounded preceding and unbounded following)或者 over(order by salary rows between unbounded preceding and unbounded following):窗口不做限制
select opr_id, frist_value(route_name ignorenulls) over(orderby opr_id) from rm_circuit_route where opr_id = '0000123'
lag() over()函数用法(取出前n行数据)
lag(expresstion,<offset>,<default>)
1 2 3 4 5 6 7 8 9 10 11 12
with a as (select1id,'a'namefrom dual union select2id,'b'namefrom dual union select3id,'c'namefrom dual union select4id,'d'namefrom dual union select5id,'e'namefrom dual ) selectid,name,lag(id,1,'')over(orderbyname) from a;
lead() over()函数用法(取出后N行数据)
lead(expresstion,<offset>,<default>)
1 2 3 4 5 6 7 8 9 10 11 12
with a as (select1id,'a'namefrom dual union select2id,'b'namefrom dual union select3id,'c'namefrom dual union select4id,'d'namefrom dual union select5id,'e'namefrom dual ) selectid,name,lead(id,1,'')over(orderbyname) from a;
with a as (select1 a from dual unionall select1 a from dual unionall select1 a from dual unionall select2 a from dual unionall select3 a from dual unionall select4 a from dual unionall select4 a from dual unionall select5 a from dual ) select a, ratio_to_report(a)over(partitionby a) b from a orderby a;
with a as (select1 a from dual unionall select1 a from dual unionall select1 a from dual unionall select2 a from dual unionall select3 a from dual unionall select4 a from dual unionall select4 a from dual unionall select5 a from dual ) select a, ratio_to_report(a)over() b from a --分母缺省就是整个占比 orderby a;
with a as (select1 a from dual unionall select1 a from dual unionall select1 a from dual unionall select2 a from dual unionall select3 a from dual unionall select4 a from dual unionall select4 a from dual unionall select5 a from dual ) select a, ratio_to_report(a)over() b from a groupby a orderby a;--分组后的占比
SELECT ename, sal, deptno, percentile_disc(0.7) withinGROUP(ORDERBY sal) over(PARTITIONBY deptno) "Percentile_Disc", cume_dist() over(PARTITIONBY deptno ORDERBY sal) "Cume_Dist" FROM emp WHERE deptno IN (30, 60);
示例
按 zjhm,xm 分组, ywscsj 降序,取第一条记录
1 2 3 4 5 6
select aa.* from (select t.* ROW_NUMBER() over(partitionby t.zjhm, t.xm orderby t.ywscsj desc) as g_id from hiop.tb_yl_patient_information t) aa where aa.g_id = 1