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

边栏推荐
- C语言实现发牌功能基本方法
- 提高shuffle操作中的reduce并行度
- 如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
- mysql如果计算本月变动/本月增幅/同比变动/同比增幅?
- Excel VBA: realize automatic drop-down filling formula to the last line
- 提升命令行效率的 Bash 快捷键 [完整版]
- A material of machine learning
- C language force buckle question 42 of rain. Four methods - violence, dynamic planning, stack, double pointer
- 【Leetcode】493. Reverse Pairs
- Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
猜你喜欢

地球系统模式(CESM)实践技术

Embedded sharing collection 21

Install nccl \ mpirun \ horovod \ NVIDIA tensorflow (3090ti)

公交站间的距离 : 简单模拟题

Unity scene jump script

一次线上事故,我顿悟了异步的精髓

Learn to map with nature medicine -- complex heat map

【ACWing】2983. 玩具

新导则下的防洪评价报告编制方法及洪水建模

Uniapp applet framework - a set of code, multi segment coverage
随机推荐
Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
35. 搜索插入位置
[acwing] 1268. Simple questions
The elderly who claim alimony from other children after being supported by their widowed daughter-in-law should be supported
Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
Distance between bus stops: simple simulation problem
Go-Excelize API源码阅读(六)—— DeleteSheet(sheet string)
Date and time function of MySQL function summary
Getaverse,走向Web3的远方桥梁
Test of countlaunch demo
The pit of history can only be filled up as far as possible
Textfield and password input box that are more flexible and easy to use in compose
I talked with the interviewer about MySQL optimization in five dimensions
Earth system model (cesm) practical technology
MySQL basic learning
npm操作指令
C语言详解系列——函数的认识(3)形参,实参,嵌套调用和链式访问
Nacos registry
ThreadLocal transfer between parent and child threads in asynchronous
Webassembly 01 basic information