sql实现同比,环比sql
环比和同比用于描述统计数据的变化状况。微信
公式:flex
SQL图片示例:
spa
SQL代码:.net
select
3dt1.ptdate
code,t1.area
orm,t1.goods_name
blog,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源创计划”,欢迎正在阅读的你也加入,一块儿分享。