当前位置: 移动技术网 > IT编程>数据库>Oracle > ORACLE开窗函数语法和使用介绍

ORACLE开窗函数语法和使用介绍

2018年09月14日  | 移动技术网IT编程  | 我要评论

八马大红袍,文水县人民政府,黄梅天装修

语法:row_number() over(partition by ...)

分组取最大的objid那一条,根据objid倒叙排序,取rn=1

select * from (select a.* ,row_number()over(partiotion by col1,col2 order by col1,col2) r from tablename ) where r=1;

分析函数和聚合函数的不同之处是什么?

普通的聚合函数用group by分组,每个分组返回一个统计值,而分析函数采用partition by分组,并且每组每行都可以返回一个统计值。

分析函数的形式

分析函数带有一个开窗函数over(),包含三个分析子句:分组(partition by), 排序(order by), 窗口(rows) ,他们的使用形式如下:over(partition by xxx order by yyy rows between zzz)

--利用开窗函数取每组数据的最后一条数据

sql>select * from (select row_number() over (partition by accountid order by updatetimestamp desc) rn, plm_openaccountrecon.* from plm_openaccountrecon) where rn = 1 and t.accountid = :accountid

--unbounded preceding and unbouned following针对当前所有记录的前一条、后一条记录,也就是表中的所有记录

--unbounded:不受控制的,无限的

--preceding:在...之前

--following:在...之后

--第一行到当前行

sql> select t.objid, t.applyempid, t.applydate, t.status, t.leavetype, t.leavedays, sum(leavedays) over(partition by applyempid order by objid rows between unbounded preceding and current row) lds from rt_gtcxleave t;

--当前行至最后一行

sql> select t.objid, t.applyempid, t.applydate, t.status, t.leavetype, t.leavedays, sum(leavedays) over(partition by applyempid order by objid rows between current row and unbounded following) lds from rt_gtcxleave t;

--当前行的上一行 (rownum - 1) 到当前行

--between 1 preceding and current row

sql> select t.objid, t.applyempid, t.applydate, t.status, t.leavetype, t.leavedays, sum(leavedays) over(partition by applyempid order by objid rows between 1 preceding and current row) lds from rt_gtcxleave t;

--当前行的上一行 (rownum - 1) 到当前行的下两行 (rownum + 2)

--rows between 1 preceding and 2 following

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网