1、同比环比
---- 练习 同比环比create table sales_amount(years varchar2(20), months varchar2(20), sales_amount number);insert into sales_amount values('2016','01',100);insert into sales_amount values('2016','02',110);insert into sales_amount values('2016','03',140);insert into sales_amount values('2016','04',150);insert into sales_amount values('2016','05',190);insert into sales_amount values('2016','06',300);insert into sales_amount values('2016','07',450);insert into sales_amount values('2016','08',580);insert into sales_amount values('2016','09',470);insert into sales_amount values('2016','10',340);insert into sales_amount values('2016','11',240);insert into sales_amount values('2016','12',180);insert into sales_amount values('2017','01',130);insert into sales_amount values('2017','02',150);insert into sales_amount values('2017','03',190);insert into sales_amount values('2017','04',250);insert into sales_amount values('2017','05',390);insert into sales_amount values('2017','06',800);insert into sales_amount values('2017','07',950);insert into sales_amount values('2017','08',380);insert into sales_amount values('2017','09',570);insert into sales_amount values('2017','10',740);insert into sales_amount values('2017','11',340);insert into sales_amount values('2017','12',680);COMMIT;---- 目标表展示效果一年 1月销售额 2月销售额 3月销售额 ...... 12月销售额2017 100 110 140 180展示效果二年 1季度销售额 2季度销售额 3季度销售额 4季度销售额求:月增长率 季度增长率? -- 环比 相邻月份 举例(二月份销售额 - 一月份销售额)/一月份销售额-- 同比 相邻年份的相同月份 (2017 一月份销售额 - 2016 一月份销售额) / 2016 一月份销售额--------------------------------------------------------------- 行转列-- deoce 月度展示select s.years, sum(decode(s.months, '01', s.sales_amount, 0)) as m1, sum(decode(s.months, '02', s.sales_amount, 0)) as m2, sum(decode(s.months, '03', s.sales_amount, 0)) as m3, sum(decode(s.months, '04', s.sales_amount, 0)) as m4, sum(decode(s.months, '05', s.sales_amount, 0)) as m5, sum(decode(s.months, '06', s.sales_amount, 0)) as m6, sum(decode(s.months, '07', s.sales_amount, 0)) as m7, sum(decode(s.months, '08', s.sales_amount, 0)) as m8, sum(decode(s.months, '09', s.sales_amount, 0)) as m9, sum(decode(s.months, '10', s.sales_amount, 0)) as m10, sum(decode(s.months, '11', s.sales_amount, 0)) as m11, sum(decode(s.months, '12', s.sales_amount, 0)) as m12 from sales_amount s group by s.years;-- case when 季度展示select s.years ,sum(case when s.months in('01','02','03') then s.sales_amount else 0 end ) as Q1 ,sum(case when s.months in('04','05','06') then s.sales_amount else 0 end ) as Q2 ,sum(case when s.months in('07','08','09') then s.sales_amount else 0 end ) as Q3 ,sum(case when s.months in('10','11','12') then s.sales_amount else 0 end ) as Q4 from sales_amount s group by s.years;-- 季度环比 相邻-- Q4 下移一行 分析函数 lagwith tmp as(select s.years ,sum(case when s.months in('01','02','03') then s.sales_amount else 0 end ) as Q1 ,sum(case when s.months in('04','05','06') then s.sales_amount else 0 end ) as Q2 ,sum(case when s.months in('07','08','09') then s.sales_amount else 0 end ) as Q3 ,sum(case when s.months in('10','11','12') then s.sales_amount else 0 end ) as Q4 from sales_amount s group by s.years)select t.* ,lag(Q4) over(partition by null order by years) as new_Q4 from tmp t;-- with tmp as(select s.years ,sum(case when s.months in('01','02','03') then s.sales_amount else 0 end ) as Q1 ,sum(case when s.months in('04','05','06') then s.sales_amount else 0 end ) as Q2 ,sum(case when s.months in('07','08','09') then s.sales_amount else 0 end ) as Q3 ,sum(case when s.months in('10','11','12') then s.sales_amount else 0 end ) as Q4 from sales_amount s group by s.years)select t.years ,(t.Q1 - lag(Q4) over(partition by null order by years))/lag(Q4) over(partition by null order by years) as Q1_HB_RATE ,(t.Q2 - t.Q1)/t.Q1 as Q2_HB_RATE ,(t.Q3 - t.Q2)/t.Q2 as Q3_HB_RATE ,(t.Q4 - t.Q3)/t.Q3 as Q4_HB_RATE from tmp t; -- 月份环比 相邻with tmp as(select s.years ,sum(decode(s.months,'01',s.sales_amount,0)) as m1 ,sum(decode(s.months,'02',s.sales_amount,0)) as m2 ,sum(decode(s.months,'03',s.sales_amount,0)) as m3 ,sum(decode(s.months,'04',s.sales_amount,0)) as m4 ,sum(decode(s.months,'05',s.sales_amount,0)) as m5 ,sum(decode(s.months,'06',s.sales_amount,0)) as m6 ,sum(decode(s.months,'07',s.sales_amount,0)) as m7 ,sum(decode(s.months,'08',s.sales_amount,0)) as m8 ,sum(decode(s.months,'09',s.sales_amount,0)) as m9 ,sum(decode(s.months,'10',s.sales_amount,0)) as m10 ,sum(decode(s.months,'11',s.sales_amount,0)) as m11 ,sum(decode(s.months,'12',s.sales_amount,0)) as m12 from sales_amount s group by s.years)select t.years ,(t.m1 - lag(m12) over(partition by null order by years))/lag(m12) over(partition by null order by years) as M1_HB_RATE ,(t.m2 - t.m1)/t.m1 as M2_HB_RATE ,(t.m3 - t.m2)/t.m2 as M3_HB_RATE ,(t.m4 - t.m3)/t.m3 as M4_HB_RATE ,(t.m5 - t.m4)/t.m4 as M5_HB_RATE ,(t.m6 - t.m5)/t.m5 as M6_HB_RATE ,(t.m7 - t.m6)/t.m6 as M7_HB_RATE ,(t.m8 - t.m7)/t.m7 as M8_HB_RATE ,(t.m9 - t.m8)/t.m8 as M9_HB_RATE ,(t.m10 - t.m9)/t.m9 as M10_HB_RATE ,(t.m11 - t.m10)/t.m10 as M11_HB_RATE ,(t.m12 - t.m11)/t.m11 as M12_HB_RATE from tmp t;-- 同比 季度with tmp as(select s.years ,sum(case when s.months in('01','02','03') then s.sales_amount else 0 end ) as Q1 ,sum(case when s.months in('04','05','06') then s.sales_amount else 0 end ) as Q2 ,sum(case when s.months in('07','08','09') then s.sales_amount else 0 end ) as Q3 ,sum(case when s.months in('10','11','12') then s.sales_amount else 0 end ) as Q4 from sales_amount s group by s.years)select t.years ,(t.Q1 - lag(Q1) over(partition by null order by years))/lag(Q1) over(partition by null order by years) as Q1_TB_RATE ,(t.Q2 - lag(Q2) over(partition by null order by years))/lag(Q2) over(partition by null order by years) as Q1_TB_RATE ,(t.Q3 - lag(Q3) over(partition by null order by years))/lag(Q3) over(partition by null order by years) as Q1_TB_RATE ,(t.Q4 - lag(Q4) over(partition by null order by years))/lag(Q4) over(partition by null order by years) as Q1_TB_RATE from tmp t;-- 同比 月份with tmp as(select s.years ,sum(decode(s.months,'01',s.sales_amount,0)) as m1 ,sum(decode(s.months,'02',s.sales_amount,0)) as m2 ,sum(decode(s.months,'03',s.sales_amount,0)) as m3 ,sum(decode(s.months,'04',s.sales_amount,0)) as m4 ,sum(decode(s.months,'05',s.sales_amount,0)) as m5 ,sum(decode(s.months,'06',s.sales_amount,0)) as m6 ,sum(decode(s.months,'07',s.sales_amount,0)) as m7 ,sum(decode(s.months,'08',s.sales_amount,0)) as m8 ,sum(decode(s.months,'09',s.sales_amount,0)) as m9 ,sum(decode(s.months,'10',s.sales_amount,0)) as m10 ,sum(decode(s.months,'11',s.sales_amount,0)) as m11 ,sum(decode(s.months,'12',s.sales_amount,0)) as m12 from sales_amount s group by s.years)select t.years ,(t.m1 - lag(m1) over(partition by null order by years))/lag(m1) over(partition by null order by years) as M1_TB_RATE ,(t.m2 - lag(m2) over(partition by null order by years))/lag(m2) over(partition by null order by years) as M2_TB_RATE ,(t.m3 - lag(m3) over(partition by null order by years))/lag(m3) over(partition by null order by years) as M3_TB_RATE ,(t.m4 - lag(m4) over(partition by null order by years))/lag(m4) over(partition by null order by years) as M4_TB_RATE ,(t.m5 - lag(m5) over(partition by null order by years))/lag(m5) over(partition by null order by years) as M5_TB_RATE ,(t.m6 - lag(m6) over(partition by null order by years))/lag(m6) over(partition by null order by years) as M6_TB_RATE ,(t.m7 - lag(m7) over(partition by null order by years))/lag(m7) over(partition by null order by years) as M7_TB_RATE ,(t.m8 - lag(m8) over(partition by null order by years))/lag(m8) over(partition by null order by years) as M8_TB_RATE ,(t.m9 - lag(m9) over(partition by null order by years))/lag(m9) over(partition by null order by years) as M9_TB_RATE ,(t.m10 - lag(m10) over(partition by null order by years))/lag(m10) over(partition by null order by years) as M10_TB_RATE ,(t.m11 - lag(m11) over(partition by null order by years))/lag(m11) over(partition by null order by years) as M11_TB_RATE ,(t.m12 - lag(m12) over(partition by null order by years))/lag(m12) over(partition by null order by years) as M12_TB_RATE from tmp t;