Oracle學習筆記:環比計算


一、構建測試表

-- 創建表
create table temp_cwh_test
(
	date_time date,
	cnt number
);
-- 插入測試數據
insert into temp_cwh_test values (to_date('2019-01-05','yyyy-mm-dd'), 25);
insert into temp_cwh_test values (to_date('2019-02-05','yyyy-mm-dd'), 20);
insert into temp_cwh_test values (to_date('2019-03-05','yyyy-mm-dd'), 63);
insert into temp_cwh_test values (to_date('2019-04-05','yyyy-mm-dd'), 96);
insert into temp_cwh_test values (to_date('2019-05-05','yyyy-mm-dd'), 25);
insert into temp_cwh_test values (to_date('2019-07-05','yyyy-mm-dd'), 12);
insert into temp_cwh_test values (to_date('2019-08-05','yyyy-mm-dd'), 39);
insert into temp_cwh_test values (to_date('2019-09-05','yyyy-mm-dd'), 250);
insert into temp_cwh_test values (to_date('2019-11-05','yyyy-mm-dd'), 1);
insert into temp_cwh_test values (to_date('2019-12-05','yyyy-mm-dd'), 525);

二、環比計算

  • 概念

環比 = (第n月 - 第n-1月)/ 第n-1月 * 100%

同比 = (今年第n月 - 去年第n月)/ 去年第n月 * 100%

  • 計算

因部分數據缺失,某些月份沒有數據,此時可以構造一個基礎表。

select '2019-' || lpad(rownum, 2, '0') as col from dual connect by level <= 12;
/*
2019-01
2019-02
2019-03
2019-04
2019-05
2019-06
2019-07
2019-08
2019-09
2019-10
2019-11
2019-12
*/

再進行左關聯即可。

使用 lag 窗口函數進行數據偏移,即可計算。

select date_time,
       cnt,
	   cnt_lag,
	   cnt_diff,
	   case when (round((case when cnt = 0 then null
	          else cnt_diff/decode(cnt_lag, 0, 1, cnt_lag) end ) * 100, 2) is
			  || '%' as rate
from 
(
select a.date_time,
       nvl(b.cnt, 0) as cnt,
	   lag(nvl(b.cnt, 0), 1) over ( order by a.date_time) as cnt_lag,
 	   nvl(b.cnt, 0) - lag(nvl(b.cnt, 0), 1) over ( order by a.date_time) as cnt_diff	  
from
(
	select '2019-' || lpad(rownum, 2, '0') as date_time 
	from dual 
	connect by level <= 12
) a
left join temp_cwh_test b
on a.date_time = to_char(b.date_time,'yyyy-mm')
order by a.date_time
) d
order by date_time
/*
1	2019-01	25			
2	2019-02	20	25	-5	-20
3	2019-03	63	20	43	215
4	2019-04	96	63	33	52.38
5	2019-05	25	96	-71	-73.96
6	2019-06	0	25	-25	
7	2019-07	12	0	12	1200
8	2019-08	39	12	27	225
9	2019-09	250	39	211	541.03
10	2019-10	0	250	-250	
11	2019-11	1	0	1	100
12	2019-12	525	1	524	52400                 
*/

參考鏈接:Oracle計算環比示例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM