sql实现同比,环比
环比和同比用于描述统计数据的变化情况。
公式:
同比增长率=(本期数-同期数)÷同期数×100%
SQL图片示例:
SQL代码:
select
t1.ptdate
,t1.area
,t1.goods_name
,t1.income
,ifnull(t1.income,0)/ifnull(t2.income,0)-1 as year_on_year_income
,t1.user_count
,ifnull(t1.user_count,0)/ifnull(t2.user_count,0)-1 as year_on_year_user_count
,'同比' as mark
from bdl_goods_income_public t1
join bdl_goods_income_public t2
on(t1.area=t2.area and t1.goods_name=t2.goods_name and t2.ptdate=DATE_SUB('2019-04-22',INTERVAL 6 day))
where t1.area ='china'
and t1.ptdate = '2019-04-22'
union all
select
t1.ptdate
,t1.area
,t1.goods_name
,t1.income
,0 as year_on_year_income
,t1.user_count
,0 as year_on_year_user_count
,'核对数据' as mark
from bdl_goods_income_public t1
where t1.area ='china'
and ptdate=DATE_SUB('2019-04-22',INTERVAL 6 day)
;
同比环比公式
本文分享自微信公众号 - SQL数据分析(dianwu_dw)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。