oracle求同比,环比函数(LAG与LEAD)的详解

前端之家收集整理的这篇文章主要介绍了oracle求同比,环比函数(LAG与LEAD)的详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Lag和Lead函数可以在一次查询中取出同一字段的前N行的数据和后N行的值。这种操作可以使用对相同表的表连接来实现,不过使用LAG和LEAD有更高的效率。

301_2@ 代码如下:
CREATE TABLE salaryByMonth
(
employeeNo varchar2(20),
yearMonth varchar2(6),
salary number
) ;
insert into SALARYBYMONTH (EMPLOYEENO,YEARMONTH,SALARY)
values (1,'200805',500);
insert into SALARYBYMONTH (EMPLOYEENO,'200802',150);
insert into SALARYBYMONTH (EMPLOYEENO,'200803',200);
insert into SALARYBYMONTH (EMPLOYEENO,'200804',300);
insert into SALARYBYMONTH (EMPLOYEENO,'200708',100);
commit;

SELECT EMPLOYEENO
,YEARMONTH
,SALARY
,MIN(SALARY) KEEP(DENSE_RANK FIRST ORDER BY YEARMONTH) OVER(PARTITION BY EMPLOYEENO) FIRST_SALARY -- 基比分析 salary/first_salary
,LAG(SALARY,1,0) OVER(PARTITION BY EMPLOYEENO ORDER BY YEARMONTH) AS PREV_SAL

,12,0) OVER(PARTITION BY EMPLOYEENO ORDER BY YEARMONTH) AS PREV_12_SAL

,SUM(SALARY) OVER(PARTITION BY EMPLOYEENO,SUBSTR(YEARMONTH,4) ORDER BY YEARMONTH RANGE UNBOUNDED PRECEDING) LJ --累计值
FROM SALARYBYMONTH
ORDER BY EMPLOYEENO
,YEARMONTH

原文链接:https://www.f2er.com/oracle/65532.html

猜你在找的Oracle相关文章