当前位置:网站首页>How to calculate yoy and mom in MySQL

How to calculate yoy and mom in MySQL

2022-06-22 20:32:00 1024 Q

Catalog

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

Preface

When 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 differences

Year 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 preparation

Create 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');

Calculate year-on-year and month on month 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 asc

result :

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,mm

The year-on-year and month on month comparisons are really difficult to calculate

summary

This 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 !


原网站

版权声明
本文为[1024 Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221905135663.html