Home >Database >Mysql Tutorial >How to calculate year-on-year and month-on-month in MySQL

How to calculate year-on-year and month-on-month in MySQL

WBOY
WBOYforward
2023-05-29 09:55:114673browse

Let’s first take a look at what is year-on-year and month-on-month:

Year-over-year: usually refers to the comparison between the nth month of this year and the nth month of last year. The year-on-year development rate is mainly to eliminate the impact of seasonal changes and to illustrate the relative development rate achieved by comparing the development level of this period with the development level of the same period last year.

Monochrome is usually used to express the proportion of change in quantity within two consecutive time periods (such as two consecutive months). There are two types of chain ratio: chain growth rate and chain development speed.

How to calculate the year-on-year growth rate and the month-on-month growth rate:

Year-on-year growth rate:

Year-on-year growth rate = (number in this period− number in the same period) / same period Number∗ 100 %

Month-to-month growth rate:

Month-to-month growth rate = (Number of this period − Number of previous period) / Number of previous period × 100 %

Year-on-year and month-on-month growth The difference

  • year-on-year is the comparison between the current period and the same period, and the month-on-month comparison is the comparison between the current period and the previous period.

  • The month-on-month ratio is generally used in months and days and rarely in years. It is mainly used to compare the degree of increase in a short period of time. However, due to industry differences, such as tourism, it will be affected by the off-peak and peak seasons. .

  • YoY is generally used in two adjacent years, within the same time period, to check the degree of increase. It is generally used in the same month in two years, and rarely used on the same date in two months.

How to calculate year-on-year and month-on-month calculations in MySQL

Data preparation

Create a product table and add data to the product table

CREATE TABLE product  (
  `产品ID` varchar(20) NOT NULL,
	  `产品名称` varchar(20) ,
  `产品单价` int (10)
)
INSERT INTO product VALUES ('C1001','产品A',45);
INSERT INTO product VALUES ('C1002','产品B',52);
INSERT INTO product VALUES ('C1003','产品C',39);

How to calculate year-on-year and month-on-month in MySQL

Create an order detail table and add data to the order detail table

CREATE TABLE sales  (
    `订单ID` int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `产品ID` varchar(25) NOT NULL ,
	  `销售数量` int(20) ,
  `销售时间` timestamp(6) NULL DEFAULT NULL
);
INSERT INTO sales(`产品ID`,`销售数量`,`销售时间`) VALUES ('C1001', 15, '2020-06-01 10:10:12');
INSERT INTO sales(`产品ID`,`销售数量`,`销售时间`)  VALUES ('C1002',26, '2020-05-02 0:10:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1003', 21, '2020-04-03 0:10:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1003', 23, '2020-04-04 0:10:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1003', 0, '2020-03-05 0:10:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1001', 16, '2020-02-06 3:0:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1002', 32, '2020-01-07 0:10:12');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1001', 16, '2019-12-08 0:12:24');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1001', 32, '2019-06-09  0:12:24');
INSERT INTO sales (`产品ID`,`销售数量`,`销售时间`) VALUES ('C1002', 17, '2019-05-09 0:12:24');

How to calculate year-on-year and month-on-month in MySQL

Calculate year-on-year and month-on-month calculations

select  year(c.销售时间) yy,month(c.销售时间) mm,     
       sum(c.销售数量*d.产品单价) ss,
concat(ifnull(abs(round((sum(c.销售数量*d.产品单价)-ss1)/ss1*100,2)),0),'%') 同比,
concat(ifnull(abs(round((sum(c.销售数量*d.产品单价)-ss2)/ss2*100,2)),0),'%')  环比
from sales c
left join product d on c.产品ID=d.产品ID
left join (select month(a.销售时间) mm1,
                    year(a.销售时间) yy1,
                    sum(a.销售数量*d.产品单价) ss1
          from sales a
          left join product d on a.产品ID=d.产品ID
          GROUP BY mm1,yy1) a
          on month(c.销售时间) = a.mm1 
          and a.yy1 = year(c.销售时间)-1	 
 left join  (select month(a.销售时间) mm2,
                    year(a.销售时间) yy2,
                    sum(a.销售数量*d.产品单价) ss2
             from sales a
		     left join product d on a.产品ID=d.产品ID
              GROUP BY mm2,yy2) b
on (b.yy2 = year(c.销售时间) and b.mm2+1 = month(c.销售时间) OR (yy2=year(c.销售时间)-1 
AND b.mm2 = 12 AND month(c.销售时间) = 1))
 group by yy, mm
 order by yy,mm asc

sql Analysis

select  year(c.销售时间) yy,month(c.销售时间) mm,     
       sum(c.销售数量*d.产品单价) ss,
 
# concat函数,mysql字符串拼接,因为同比和环比都是百分数
# ifnull函数,mysql判断字段是否为空,为空则为0
# abs函数,mysql取绝对值,因为我这里取的都是正数
# round函数,mysql保留几位小数
concat(ifnull(abs(round((sum(c.销售数量*d.产品单价)-ss1)/ss1*100,2)),0),'%') 同比,
concat(ifnull(abs(round((sum(c.销售数量*d.产品单价)-ss2)/ss2*100,2)),0),'%')  环比
from sales c
left join product d on c.产品ID=d.产品ID
 
# 上一年销售额
left join (select month(a.销售时间) mm1,
                    year(a.销售时间) yy1,
                    sum(a.销售数量*d.产品单价) ss1
          from sales a
          left join product d on a.产品ID=d.产品ID
          GROUP BY mm1,yy1) a
          # 同比月份相同,年份减1
          on month(c.销售时间) = a.mm1   
          and a.yy1 = year(c.销售时间)-1	
 
# 今年销售额 
 left join  (select month(a.销售时间) mm2,
                    year(a.销售时间) yy2,
                    sum(a.销售数量*d.产品单价) ss2
             from sales a
		     left join product d on a.产品ID=d.产品ID
              GROUP BY mm2,yy2) b
          # 环比取数考虑到为一月的情况
on (b.yy2 = year(c.销售时间) and b.mm2+1 = month(c.销售时间) OR (yy2=year(c.销售时间)-1 
AND b.mm2 = 12 AND month(c.销售时间) = 1))
 group by yy, mm
 order by yy,mm asc

Result:

How to calculate year-on-year and month-on-month in MySQL

In addition, if you want to calculate cumulative sales, you can write like this:

select year(销售时间) yy,month(销售时间) mm,
sum(销售数量*b.产品单价) over(order by year(销售时间) ,month(销售时间) ) 累计数量 
from sales a
left join product b on a.产品ID=b.产品ID
order by  yy,mm

The above is the detailed content of How to calculate year-on-year and month-on-month in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete