当前位置:网站首页>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;

边栏推荐
- MySQL基础学习
- 五个维度着手MySQL的优化,我和面试官都聊嗨了
- Yolov5 implementation process - Directory
- Redis过期删除策略和内存淘汰策略
- 【洛谷】P3919 【模板】可持久化线段树1(可持久化数组)
- BigDecimal 的 4 个坑,你踩过几个?
- Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
- Distance between bus stops: simple simulation problem
- 基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习
- Shell read read console input, use of read
猜你喜欢
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题

MySQL eight knowledge points: from getting started to deleting the database

JVM第五讲:纵横数据如何应对洪峰推送

Textfield and password input box that are more flexible and easy to use in compose

Uniapp applet framework - a set of code, multi segment coverage

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

Please elaborate on the implementation principle of synchronized and related locks

Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)

Ansible中常用的模块

Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)
随机推荐
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
Webassembly 01 basic information
Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health
Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
安装NCCL\mpirun\horovod\nvidia-tensorflow(3090Ti)
【ACWing】1268. 简单题
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology
JVM Lecture 2: class loading mechanism
Annotation @autowired how to assemble automatically
Practical technology of SWAT Model in simulation of hydrology, water resources and non-point source pollution
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
五个维度着手MySQL的优化,我和面试官都聊嗨了
[weekly translation go] how to write your first program with go
Your understanding of the "happen before principle" may be wrong?
Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
MODFLOW Flex、GMS、FEFLOW、HYDRUS实践应用
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式