当前位置:网站首页>How to calculate yoy and mom in MySQL
How to calculate yoy and mom in MySQL
2022-06-22 20:32:00 【1024 Q】
Preface
Let's first look at what is year-on-year , What is chain comparison :
How to calculate the year-on-year growth rate and the month on month growth rate :
Year on year and month on month differences
stay MySQL How to calculate year-on-year and month on month comparisons
Data preparation
Calculate year-on-year and month on month
sql analysis
summary
PrefaceWhen doing data modeling today ,ads There is an index called year-on-year and month on month in the demand of layer , The calculation of these two indicators has not been touched before , After twoorthree hours of research , Finally, I understand what is year-on-year , What is chain comparison , How to use Mysql To calculate year-on-year and month on month comparisons .
Let's first look at what is year-on-year , What is chain comparison :Year on year : It usually refers to the... Of this year n Month and the... Of last year n Monthly comparison . The year-on-year growth rate is mainly to eliminate the impact of seasonal changes , It is used to explain the relative development speed achieved by comparing the development level of this period with that of the same period last year .
Chain ratio : It usually means continuous 2 One unit period ( Like two months in a row ) The change of the quantity in is more than . The chain comparison includes two types : Month on month growth rate and month on month development rate .
How to calculate the year-on-year growth rate and the month on month growth rate :Year on year growth rate :
Same as Than increase Long rate = ( Ben period Count − Same as period Count ) / Same as period Count ∗ 100 %
Month on month growth rate :
Ring Than increase Long rate = ( Ben period Count − On period Count ) / On period Count × 100 %
Year on year and month on month differencesYear on year is the comparison between the current period and the same period , The month on month comparison is the comparison between the current period and the previous period .
The month on month comparison is generally used for 、 Days are seldom used for years , It is mainly to compare the degree of increase in a very short time , However, due to industry differences , Such as tourism , It will be affected by the low and peak seasons .
Year on year is generally used in the next two years , In the same period of time , View the increase degree , It is usually used in the same month of two years , Rarely used on the same date in two months .
stay MySQL How to calculate year-on-year and month on month comparisons Data preparationCreate a product table and add data to the product table
CREATE TABLE product ( ` product ID` varchar(20) NOT NULL, ` The product name ` varchar(20) , ` Unit price of product ` int (10))INSERT INTO product VALUES ('C1001',' product A',45);INSERT INTO product VALUES ('C1002',' product B',52);INSERT INTO product VALUES ('C1003',' product C',39);
Create an order detail table and add data to the order detail table
CREATE TABLE sales ( ` Order ID` int NOT NULL PRIMARY KEY AUTO_INCREMENT, ` product ID` varchar(25) NOT NULL , ` sales volumes ` int(20) , ` Sales time ` timestamp(6) NULL DEFAULT NULL);INSERT INTO sales(` product ID`,` sales volumes `,` Sales time `) VALUES ('C1001', 15, '2020-06-01 10:10:12');INSERT INTO sales(` product ID`,` sales volumes `,` Sales time `) VALUES ('C1002',26, '2020-05-02 0:10:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1003', 21, '2020-04-03 0:10:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1003', 23, '2020-04-04 0:10:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1003', 0, '2020-03-05 0:10:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1001', 16, '2020-02-06 3:0:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1002', 32, '2020-01-07 0:10:12');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1001', 16, '2019-12-08 0:12:24');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1001', 32, '2019-06-09 0:12:24');INSERT INTO sales (` product ID`,` sales volumes `,` Sales time `) VALUES ('C1002', 17, '2019-05-09 0:12:24');
select year(c. Sales time ) yy,month(c. Sales time ) mm, sum(c. sales volumes *d. Unit price of product ) ss,concat(ifnull(abs(round((sum(c. sales volumes *d. Unit price of product )-ss1)/ss1*100,2)),0),'%') Year on year ,concat(ifnull(abs(round((sum(c. sales volumes *d. Unit price of product )-ss2)/ss2*100,2)),0),'%') Chain ratio from sales cleft join product d on c. product ID=d. product IDleft join (select month(a. Sales time ) mm1, year(a. Sales time ) yy1, sum(a. sales volumes *d. Unit price of product ) ss1 from sales a left join product d on a. product ID=d. product ID GROUP BY mm1,yy1) a on month(c. Sales time ) = a.mm1 and a.yy1 = year(c. Sales time )-1 left join (select month(a. Sales time ) mm2, year(a. Sales time ) yy2, sum(a. sales volumes *d. Unit price of product ) ss2 from sales a left join product d on a. product ID=d. product ID GROUP BY mm2,yy2) bon (b.yy2 = year(c. Sales time ) and b.mm2+1 = month(c. Sales time ) OR (yy2=year(c. Sales time )-1 AND b.mm2 = 12 AND month(c. Sales time ) = 1)) group by yy, mm order by yy,mm ascsql analysis select year(c. Sales time ) yy,month(c. Sales time ) mm, sum(c. sales volumes *d. Unit price of product ) ss,# concat function ,mysql String splicing , Because both year-on-year and month on month are percentages # ifnull function ,mysql Judge whether the field is empty , If it is empty, it is 0# abs function ,mysql Take the absolute value , Because I take positive numbers here # round function ,mysql Keep a few decimal places concat(ifnull(abs(round((sum(c. sales volumes *d. Unit price of product )-ss1)/ss1*100,2)),0),'%') Year on year ,concat(ifnull(abs(round((sum(c. sales volumes *d. Unit price of product )-ss2)/ss2*100,2)),0),'%') Chain ratio from sales cleft join product d on c. product ID=d. product ID# Sales of the previous year left join (select month(a. Sales time ) mm1, year(a. Sales time ) yy1, sum(a. sales volumes *d. Unit price of product ) ss1 from sales a left join product d on a. product ID=d. product ID GROUP BY mm1,yy1) a # Same month as last year , Years minus 1 on month(c. Sales time ) = a.mm1 and a.yy1 = year(c. Sales time )-1# This year's sales left join (select month(a. Sales time ) mm2, year(a. Sales time ) yy2, sum(a. sales volumes *d. Unit price of product ) ss2 from sales a left join product d on a. product ID=d. product ID GROUP BY mm2,yy2) b # The month on month data retrieval is considered to be in January on (b.yy2 = year(c. Sales time ) and b.mm2+1 = month(c. Sales time ) OR (yy2=year(c. Sales time )-1 AND b.mm2 = 12 AND month(c. Sales time ) = 1)) group by yy, mm order by yy,mm ascresult :

besides , If you want to calculate cumulative sales , It can be written like this :
select year( Sales time ) yy,month( Sales time ) mm,sum( sales volumes *b. Unit price of product ) over(order by year( Sales time ) ,month( Sales time ) ) Cumulative quantity from sales aleft join product b on a. product ID=b. product IDorder by yy,mmThe year-on-year and month on month comparisons are really difficult to calculate
summaryThis is about MySQL This is how to calculate the year-on-year and month on month comparisons in , More about MySQL To calculate the year-on-year and month on month content, please search the previous articles of SDN or continue to browse the following related articles. I hope you will support SDN more in the future !
边栏推荐
- 从感知机到Transformer,一文概述深度学习简史
- 运用span-method巧妙实现多层table数据的行合并
- mysql8.0忘记密码的详细解决方法
- Random talk about redis source code 122
- MySQL高级(二)
- 完全背包如何考慮排列問題
- 一个支持IPFS的电子邮件——SKIFF
- Teach you how to create SSM project structure in idea
- Cloud computing in the metauniverse to enhance your digital experience
- R语言数据预处理、把类型变量转化为因子变量,把数据集转化为h2o格式、数据集划分(训练集、测试集、验证集)
猜你喜欢

What can the accelerated implementation of digital economy bring to SMEs?

Teach you how to create SSM project structure in idea
2019 年总结:31岁,不过是另一个开始

IVX no code challenge five second game production

510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming

Introduction of Neural Network (BP) in Intelligent Computing

Security policy and NAT (easy IP) of firewall Foundation

年中大促 | 集成无忧,超值套餐 6 折起

CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务

3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?
随机推荐
三维天地助力实验室夯实完整质量体系管理
3个月自学自动化测试,薪资从4.5K到15K,鬼知道我经历了什么?
【深入理解TcaplusDB技术】单据受理之创建游戏区
Web technology sharing | [Gaode map] to realize customized track playback
从感知机到Transformer,一文概述深度学习简史
CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
市场开始降温,对NFT 是坏事么?
【深入理解TcaplusDB技术】创建游戏区
MySQL Basics - functions
[in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
他98年的,我玩不过他...
Nestjs integrates config module and Nacos to realize configuration unification
An error is reported when idea writes JSP code, but it is solved by normal operation
Comment le sac à dos complet considère - t - il la disposition?
[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary
Random talk about redis source code onehundredandtwenty
【深入理解TcaplusDB技术】TcaplusDB 表管理——清理表
Hash table (hash table)
CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务