gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章oracle求同比,环比函数(LAG与LEAD)的详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

复制代码代码如下:

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, YEARMONTH, SALARY) values (1, '200802', 150); insert into SALARYBYMONTH (EMPLOYEENO, YEARMONTH, SALARY) values (1, '200803', 200); insert into SALARYBYMONTH (EMPLOYEENO, YEARMONTH, SALARY) values (1, '200804', 300); insert into SALARYBYMONTH (EMPLOYEENO, YEARMONTH, SALARY) values (1, '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 -- 环比分析,与上个月份进行比较        ,LAG(SALARY, 12, 0) OVER(PARTITION BY EMPLOYEENO ORDER BY YEARMONTH) AS PREV_12_SAL -- 同比分析,与上个年度相同月份进行比较           ,SUM(SALARY) OVER(PARTITION BY EMPLOYEENO, SUBSTR(YEARMONTH, 1, 4) ORDER BY YEARMONTH RANGE UNBOUNDED PRECEDING) LJ --累计值   FROM SALARYBYMONTH  ORDER BY EMPLOYEENO          ,YEARMONTH 。

  。

最后此篇关于oracle求同比,环比函数(LAG与LEAD)的详解的文章就讲到这里了,如果你想了解更多关于oracle求同比,环比函数(LAG与LEAD)的详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com