mysql如果计算本月变动/本月增幅/同比变动/同比增幅?

问题描述:

原数据

第一列是id, 第二列是日期(都是每个月的最后的一天),第三列是一个月的销售额。
比如第一行的意思是id为1001,2022年7月的销售额是500。

iddatesales
100120220731500
100120210731600
100120220630800
100220220731400
100220210731300
100220220630100
100320220731600
100320210731400
100320220630600
100420220731800
100420210731100
100420220630300

最终结果

idthis_monththis_month_saleslast_month_saleslast_year_sales本月增幅本月变动同比增幅同比变动
1001Jul-225008006000.60%300-0.17%-100
1002Jul-22400100300-0.75%-3000.33%100
1003Jul-226006004000.00%00.50%200
1004Jul-22800300100-0.63%-5007.00%700

解题思路:

创建三张临时表,表名分别为tem_this_month、tem_next_month、tem_next_year。
三张表的columns分别为:

表名columns
tem_this_month:id, date, this_month, next_month, next_year,sales
tem_next_month:id, next_month, sales
tem_next_year:id, next_year, sales

然后left join三张表

具体过程及代码

1. 创建三张临时表

1.1 第一张临时表tem_this_month

create temporary table tem_this_month as (select id,
                                                 date,
                                                 date_format(date, '%Y-%m')                             as this_month,
                                                 date_format(date_sub(date, interval -1 month), '%Y-%m') as next_month,
                                                 date_format(date_sub(date, interval -1 year), '%Y-%m')  as next_year,
                                                 sales
                                          from mysql_xox
);
select *
from tem_this_month;

在这里插入图片描述tem_this_month

1.2 第二张临时表tem_next_month

create temporary table tem_next_month as (select id,
                                                 date_format(date_sub(date, interval -1 month), '%Y-%m') as next_month,
                                                 sales
                                          from mysql_xox
);
select *
from tem_next_month;

在这里插入图片描述 tem_next_month

1.3 第三张临时表tem_next_year

create temporary table tem_next_year as (select id,
                                                    date_format(date_sub(date, interval -1 year), '%Y-%m') as 'next_year',
                                                    sales
                                             from mysql_xox
);
select *
from tem_next_year;

在这里插入图片描述tem_next_year

2. left join三张表并计算

select a.id,
       a.this_month,
       date_format(date_sub(a.date, interval 1 month), '%Y-%m') as last_month,
        date_format(date_sub(a.date, interval 1 year), '%Y-%m')  as last_year,
       a.sales                                                                                 as this_month_sales,
       b.sales                                                                                 as last_month_sales,
       c.sales                                                                                 AS last_year_sales,
       case
           when a.sales is not null and b.sales is not null
               then concat(round((b.sales - a.sales) / a.sales,2),'%') end                                      as 本月增幅,
       case
           when a.sales is not null and b.sales is not null
               then b.sales - a.sales end                                                    as 本月变动,
       case
           when this_month is not null and c.sales is not null
               then concat(round((a.sales - c.sales) / c.sales,2),'%') end                                      as 同比增幅,
       case when this_month is not null and c.sales is not null then a.sales - c.sales end as 同比变动

from tem_this_month a
         left join tem_next_month b on a.id = b.id and a.this_month = b.next_month
         left join tem_next_year c on a.id = c.id and a.this_month = c.next_year;

在这里插入图片描述

精简版:

select a.id,
       a.this_month,
       a.sales                                                                                 as this_month_sales,
       b.sales                                                                                 as last_month_sales,
       c.sales                                                                                 AS last_year_sales,
       case
           when a.sales is not null and b.sales is not null
               then concat(round((b.sales - a.sales) / a.sales,2),'%') end                                      as 本月增幅,
       case
           when a.sales is not null and b.sales is not null
               then b.sales - a.sales end                                                    as 本月变动,
       case
           when this_month is not null and c.sales is not null
               then concat(round((a.sales - c.sales) / c.sales,2),'%') end                                      as 同比增幅,
       case when this_month is not null and c.sales is not null then a.sales - c.sales end as 同比变动

from tem_this_month a
         left join tem_next_month b on a.id = b.id and a.this_month = b.next_month
         left join tem_next_year c on a.id = c.id and a.this_month = c.next_year
where b.sales is not null and c.sales is not null;

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LucaTech

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值