当前位置:网站首页>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 !
边栏推荐
- Summary of 2019: 31 is just another start
- He was in '98. I can't play with him
- Random talk about redis source code onehundredandtwenty
- 采用QTest进行数据集测试-性能测试-GUI测试
- 芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022
- LORA技术---LoRa信号从数据流变为LoRa扩频信号,再从射频信号通过解调变为数据
- Oracle system/用户被锁定的解决方法
- 理财产品在双休日可以赎回吗?
- 防火墙基础之安全策略和NAT(Easy IP)
- 软件上线前为什么要做性能测试?软件性能测试机构怎么找
猜你喜欢

Comment le sac à dos complet considère - t - il la disposition?

树莓派环境设置

智能计算之神经网络(BP)介绍

AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性

天,靠八股文逆袭了啊

芯和半导体“射频EDA/滤波器设计平台”闪耀IMS2022

Using qtest for data set test performance test GUI test
![[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary

Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)

防火墙基础之安全策略和NAT(Easy IP)
随机推荐
Random talk about redis source code 122
ROS从入门到精通(八) 常用传感器与消息数据
UnityEditor 编辑器脚本执行菜单
【深入理解TcaplusDB知识库】部署TcaplusDB Local版常见问题
什么?你居然不会微信分身
[deeply understand tcapulusdb technology] realize tcapulusdb transaction management in the operation and maintenance platform
Be careful with MySQL filesort
MySQL foundation - constraints
Security policy and NAT (easy IP) of firewall Foundation
Merge sort (recursive and iterative Implementation)
Please describe the whole process from entering a URL in the browser to rendering the page.
MySQL高级(二)
[deeply understand tcapulusdb technology] create a game area for document acceptance
智能计算之神经网络(BP)介绍
采用QTest进行数据集测试-性能测试-GUI测试
Understand the index of like in MySQL
Recv function with timeout
2019 年总结:31岁,不过是另一个开始
R语言基于h2o包构建二分类模型:使用h2o.glm构建正则化的逻辑回归模型、使用h2o.auc计算模型的AUC值
一张图解码 OpenCloudOS 社区开放日