求问Mysql同比环比SQL语句怎么写

求问Mysql同比环比SQL语句怎么写,第1张

首先明确概念:

环比增长率=(本期数-上期数)/上期数*100% 反映本期比上期增长了多少。

同比增长率=(本期数-同期数)/同期数*100% 指和去年同期相比较的增长率。

select 月份,(SUM(case when 年份=2015 then 出口量 else 0 end)-SUM(case when 年份=2014 then 出口量 else 0 end))*100.0/SUM(case when 年份=2014 then 出口量 else 0 end) 增长率百分比

    from [hgsj].[dbo].[seamless]

       where 年份 in(2014,2015)

          group by 月份

以A表为例子

先建立一个table0

create table_0

(n_date date,

sheng varchar2(20),

sale number

tongbi number)

create unique index table_0_U1 on table_0 (n_date,sheng)

create or replace package body pkg_b_tongji is

procedure sp_update_party_rating(p_sdate number, p_edate number) is

v_sdate date default to_date(p_sdate,'yyyymmdd')

v_edate date default to_date(p_edate,'yyyymmdd')

v_sqlUpd varchar2(3000)

v_sqlIns varchar2(3000)

begin

v_sqlIns := 'insert into table_0(n_date,sheng,sale)

values(:v1,:v2,:v3)'

v_sqlUpd := 'update table_0 t set sale = :v1

where n_date = :v2 and sheng = :v3'

for c1 in (select a.ndate,

a.sheng,

sum(sale) sale

from a

where ndate between v_sdate and v_edate

group by a.ndate,

a.sheng

)

loop

execute immediate v_sqlUpd using c1.sale

if sql%rowcount<=0 then --如果更新 *** 作没有执行就执行插入 *** 作

execute immediate v_sqlIns using c1.n_date,c1.sheng,c1.sale

end if

end loop

commit

end sp_update_party_rating

---更新同比

procedure sp_update_tongbi is

begin

for c2 in (

select n_date,

sheng,

sale,

nvl(sale,0) sale1

from table_0 a

left join

(select n_date,sheng,a.nvl(sale,0) sale

from table_0 a,

(select t.n_date,sheng

add_months(n_date,-1) n_date2

from table_0 t)

where a.sheng = b.sheng and a.n_date = b.n_date2)

)

loop

update table_0

set tongbi = sale/sale1

where n_date = c2.n_date and sheng = c2.sheng

commit

end loop

end sp_update_tongbi

end pkg_b_tongji


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7271352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存