当前位置:网站首页>【Oracle SQL】计算同比与环比(列转行进行偏移)
【Oracle SQL】计算同比与环比(列转行进行偏移)
2022-07-26 05:53:00 【江中洲】
关于oracle计算同比环比参考博客
--仅仅查询出来了月度和年度变化数据还需要进行计算
SELECT
t2.D_DATE,
t2.D_TASKS,
LAG ( D_TASKS, 1, 0 ) OVER ( ORDER BY t2.D_DATE ) AS monthOnMonth,
LAG ( t2.D_TASKS, 12, 0 ) OVER ( ORDER BY t2.D_DATE ) AS yearOnYear
FROM
(
SELECT
t0.D_DATE,
t1.D_TASKS
FROM
( SELECT TO_CHAR( add_months( SYSDATE,- ROWNUM ), 'yyyy-MM' ) D_DATE FROM dual CONNECT BY 13 >= ROWNUM ) t0
LEFT JOIN ( SELECT SUM( D_TASKS ) D_TASKS, D_DATE FROM ZZ_SYSTEM_DATA WHERE STYPE_NAME = '千人负伤率‰(当期年累计)' GROUP BY D_DATE ORDER BY D_DATE ) t1 ON t0.D_DATE = t1.D_DATE
ORDER BY
t0.D_DATE
) t2
还要考虑是不是为0或者为空,为0为null 做除法更麻烦了
SELECT
t3.YEARMONTH,
t3.DATA,
CASE
WHEN t3.DATA IS NULL THEN
'0.00%' ELSE TO_CHAR(
NVL(
ROUND(
DECODE( t3.LASTMONTH, 0, 100, ( t3.DATA - t3.LASTMONTH ) / t3.LASTMONTH * 100 ),
2
),
0
),
'fm9999999990.00'
) || '%'
END AS month_on_year,
CASE
WHEN t3.DATA IS NULL THEN
'0.00%' ELSE TO_CHAR(
NVL(
ROUND(
DECODE( t3.SameMonth, 0, 100, ( t3.DATA - t3.SameMonth ) / t3.SameMonth * 100 ),
2
),
0
),
'fm9999999990.00'
) || '%'
END AS year_on_year
FROM
(
SELECT
t2.D_DATE AS YearMonth,
t2.D_TASKS AS DATA,
LAG ( D_TASKS, 1, 0 ) OVER ( ORDER BY t2.D_DATE ) AS LastMonth,
LAG ( t2.D_TASKS, 12, 0 ) OVER ( ORDER BY t2.D_DATE ) AS SameMonth
FROM
(
SELECT
t0.D_DATE,
t1.D_TASKS
FROM
(
SELECT
TO_CHAR( add_months( add_months( SYSDATE,- ( 0- ( 12-to_char ( SYSDATE, 'mm' ) ) ) ),- ROWNUM + 1 ), 'yyyy-MM' ) D_DATE
FROM
dual CONNECT BY 24 >= ROWNUM
) t0
LEFT JOIN ( SELECT SUM( D_TASKS ) D_TASKS, D_DATE FROM ZZ_SYSTEM_DATA WHERE STYPE_NAME = '制造体系计件人数(含劳务派遣)' GROUP BY D_DATE ORDER BY D_DATE ) t1 ON t0.D_DATE = t1.D_DATE
ORDER BY
t0.D_DATE
) t2
) t3

边栏推荐
- Use latex to typeset multiple-choice test paper
- Blurring of unity pixel painting
- Another open source artifact, worth collecting and learning!
- Application and value of IVR in VoIP telephone system
- 光量子里程碑:6分钟内解决3854个变量问题
- Project topic selection reference
- [paper notes] anti packet loss joint coding for network speech steganography
- 卸载手机自带APP的操作步骤
- Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
- 日志收集分析平台搭建-1-环境准备
猜你喜欢

Talking about the practice of software defect management

Redis事务

How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects

FTP experiment and overview

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))

软件测试面试题全网独家没有之一的资深测试工程师面试题集锦

又一开源神器,值得收藏学习!
![ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’](/img/15/25ff1e544565e18319ecca85e614a2.png)
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’

Rocbossphp free open source light community system

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
随机推荐
Detailed explanation of the whole process of coding's pressure measurement operation
一招教你轻松看懂波特图
vagrant下载速度慢的解决方法
【STM32系列汇总】博主的STM32实战快速进阶之路(持续更新)
Lemon class automatic learning after all
Chapter 2 - getting started
Jupiter notebook shortcut key
A trick to teach you to easily understand Potter's map
EM and REM
Mba-day29 arithmetic - preliminary understanding of absolute value
Redis transaction
How to name the project version number? Looks like cow b
5-year-old Test Engineer - how to choose the next step?
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
flex布局
Redis publish subscription
Use latex to typeset multiple-choice test paper
Embedded general learning route arrangement
Application and value of IVR in VoIP telephone system
1.12 basis of Web Development