当前位置:网站首页>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 !
边栏推荐
- [deeply understand tcapulusdb technology] tcapulusdb table management - create a new table
- 运用span-method巧妙实现多层table数据的行合并
- 元宇宙中的云计算,提升你的数字体验
- 完全背包如何考虑排列问题
- R语言data.table导入数据实战:data.table数据列名称的重命名(rename)
- [in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
- Three months of self-taught automatic test, salary from 4.5K to 15K, who knows what I have experienced?
- MySQL foundation - constraints
- How should programmers look up dates
- 用RNN & CNN进行情感分析 - PyTorch
猜你喜欢
随机推荐
[deeply understand tcapulusdb technology] tcapulusdb table management - create a new table
Understand the index of like in MySQL
MySQL中如何计算同比和环比
MySQL foundation - constraints
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
[deeply understand tcapulusdb technology] tcapulusdb table management - rebuild table
[proteus simulation] NE555 delay circuit
[in depth understanding of tcapulusdb technology] getting started with MySQL driver
【Proteus仿真】三极管组成的H桥驱动直流电机+按键正反转控制
【深入理解TcaplusDB知識庫】部署TcaplusDB Local版常見問題
基于Sentinel的高可用限流系统的Grafana报表展示
防火墙基础之安全策略和NAT(Easy IP)
Introduction to JWT
MySQL advanced (II)
【深入理解TcaplusDB技术】创建游戏区
【Proteus仿真】NE555延时电路
Web technology sharing | [Gaode map] to realize customized track playback
mysql8.0忘记密码的详细解决方法
什么?你居然不会微信分身
Oracle system/用户被锁定的解决方法





![Web technology sharing | [Gaode map] to realize customized track playback](/img/80/7daba6716b85276de8d09b9d016313.png)

