当前位置:网站首页>If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
2022-07-26 05:15:00 【LucaTech】
Problem description :
The original data
The first column is id, The second column is the date ( It's the last day of every month ), The third column is the sales of one month .
For example, the first line means id by 1001,2022 year 7 The monthly sales volume is 500.
| id | date | sales |
|---|---|---|
| 1001 | 20220731 | 500 |
| 1001 | 20210731 | 600 |
| 1001 | 20220630 | 800 |
| 1002 | 20220731 | 400 |
| 1002 | 20210731 | 300 |
| 1002 | 20220630 | 100 |
| 1003 | 20220731 | 600 |
| 1003 | 20210731 | 400 |
| 1003 | 20220630 | 600 |
| 1004 | 20220731 | 800 |
| 1004 | 20210731 | 100 |
| 1004 | 20220630 | 300 |
final result
| id | this_month | this_month_sales | last_month_sales | last_year_sales | Growth rate of this month | Changes in this month | Year-on-year growth | Year on year changes |
|---|---|---|---|---|---|---|---|---|
| 1001 | Jul-22 | 500 | 800 | 600 | 0.60% | 300 | -0.17% | -100 |
| 1002 | Jul-22 | 400 | 100 | 300 | -0.75% | -300 | 0.33% | 100 |
| 1003 | Jul-22 | 600 | 600 | 400 | 0.00% | 0 | 0.50% | 200 |
| 1004 | Jul-22 | 800 | 300 | 100 | -0.63% | -500 | 7.00% | 700 |
Their thinking :
Create three temporary tables , The table names are tem_this_month、tem_next_month、tem_next_year.
Three tables columns Respectively :
| Table name | 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 |
then left join Three tables
Specific process and code
1. Create three temporary tables
1.1 The first provisional table 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 Second provisional table 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 The third provisional table 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 Three tables and calculate
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 Growth rate of this month ,
case
when a.sales is not null and b.sales is not null
then b.sales - a.sales end as Changes in this month ,
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 Year-on-year growth ,
case when this_month is not null and c.sales is not null then a.sales - c.sales end as Year on year changes
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;

Lite version :
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 Growth rate of this month ,
case
when a.sales is not null and b.sales is not null
then b.sales - a.sales end as Changes in this month ,
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 Year-on-year growth ,
case when this_month is not null and c.sales is not null then a.sales - c.sales end as Year on year changes
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;

边栏推荐
- Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
- 【Leetcode】493. Reverse Pairs
- YOLOv5执行全过程----目录
- [acwing] 2983. Toys
- Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
- Seata submits at details in two stages
- 遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
- Annotation @autowired how to assemble automatically
- ThreadLocal transfer between parent and child threads in asynchronous
- 推荐必读:测试人员如何快速熟悉新业务?
猜你喜欢

阿里三面:MQ 消息丢失、重复、积压问题,如何解决?

Nacos 介绍和部署

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像

Excel VBA: summarize calculation output results by date (SUMIF)

Week 6 Learning Representation: Word Embedding (symbolic →numeric)
![Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning](/img/87/9f8353c5c9c700eaa63f66697aa44a.png)
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning

DOM event flow event bubble event capture event delegate

手把手教你用代码实现SSO单点登录

Nacos registry

mysql如果计算本月变动/本月增幅/同比变动/同比增幅?
随机推荐
An online accident, I suddenly realized the essence of asynchrony
【ACWing】1268. 简单题
Excel VBA: summarize calculation output results by date (SUMIF)
SWAT模型在水文水资源、面源污染模拟中的实践技术
Computable general equilibrium (CGE) model practice technology in resource environment under the goal of "double carbon"
List converted to tree real use of the project
35. Search the insertion position
[weekly translation go] how to write your first program with go
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
Ansible中常用的模块
C语言力扣第41题之缺失的第一个正数。两种方法,预处理快排与原地哈希
Getaverse,走向Web3的远方桥梁
@Autowired注解的原理
Embedded sharing collection 21
一次线上事故,我顿悟了异步的精髓
Annotation @autowired how to assemble automatically
Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
CLM land surface process model
Date and time function of MySQL function summary