Analysis of MySQL cumulative aggregation principle and usage examples

Analysis of MySQL cumulative aggregation principle and usage examples

This article uses examples to illustrate the principles and usage of MySQL cumulative aggregation. Share with you for your reference, the details are as follows:

Cumulative aggregation aggregates data from the first element in the sequence to the current element, such as returning the cumulative number of orders and the average number of orders from the beginning of each month to the present for each employee.

There are two solutions to the row number problem, one is to use a subquery and the other is to use a join. The subquery method is usually more intuitive and readable. However, when aggregation is required, the subquery needs to scan the data once for each aggregation, while the connection method usually only needs to scan once to get the result. The following query uses a join to get the result

SELECT
 a.empid,
 a.ordermonth,a.qty AS thismonth,
 SUM(b.qty) AS total,
 CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
  ON a.empid=b.empid
  AND b.ordermonth <= a.ordermonth
GROUP BY a.empid,a.ordermonth,a.qty
ORDER BY a.empid,a.ordermonth

If you only want to query the cumulative orders in 2015, you can add the where condition

WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'

The results are as follows

You may also want to filter the data, for example, to return only the monthly orders for each employee until a certain goal is reached. Here we assume that the total number of orders for each employee is counted before it reaches 1,000.

Here we can use the HAVING filter to complete the query

SELECT
 a.empid,
 a.ordermonth,a.qty AS thismonth,
 SUM(b.qty) AS total,
 CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
  ON a.empid=b.empid
  AND b.ordermonth <= a.ordermonth
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'
GROUP BY a.empid,a.ordermonth,a.qty
HAVING total < 1000
ORDER BY a.empid,a.ordermonth

The situation in that month when the number reaches 1000 is not counted here. If we want to do statistics, the situation will be a bit complicated. If total <= 1000 is specified, statistics will be taken only if the number of orders for that month is exactly 1000. Otherwise, statistics will not be taken for that month. Therefore, the filtering of this problem can be considered from another aspect. When the cumulative cumulative orders are less than 1000, the difference between the cumulative orders and the orders of the previous month is less than 1000. At the same time, the first month with an order quantity exceeding 1000 can also be counted. Therefore, the SQL statement for this solution is as follows

SELECT
 a.empid,
 a.ordermonth,a.qty AS thismonth,
 SUM(b.qty) AS total,
 CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
  ON a.empid=b.empid
  AND b.ordermonth <= a.ordermonth
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'
GROUP BY a.empid,a.ordermonth,a.qty
HAVING total-a.qty < 1000
ORDER BY a.empid,a.ordermonth

The results are as follows

If you only want to return the data for the month with a cumulative order count of 1000, and not the previous months, you can modify the above SQL statement.

Further filter and add the condition that the cumulative order quantity is greater than or equal to 1000. The SQL statement for this problem is as follows:

SELECT
 a.empid,
 a.ordermonth,a.qty AS thismonth,
 SUM(b.qty) AS total,
 CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
  ON a.empid=b.empid
  AND b.ordermonth <= a.ordermonth
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'
GROUP BY a.empid,a.ordermonth,a.qty
HAVING total-a.qty < 1000 AND total >= 1000
ORDER BY a.empid,a.ordermonth

The results are as follows

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • Detailed explanation of MySQL commonly used aggregate functions
  • How to add conditional expressions to aggregate functions in MySql
  • php+mysql open source XNA aggregation program released for download
  • Mysql cannot select non-aggregate columns
  • Analysis of MySQL query sorting and query aggregation function usage
  • Detailed explanation of MySQL single table query operation examples [syntax, constraints, grouping, aggregation, filtering, sorting, etc.]
  • Analysis of the principle and usage of MySQL continuous aggregation
  • MySQL sliding aggregation/year-to-date aggregation principle and usage example analysis

<<:  Detailed explanation of the practical record of solving network isolation through Nginx

>>:  HTML form submission method case study

Recommend

Add ?v= version number after js or css to prevent browser caching

Copy code The code is as follows: <span style=...

Why is the scroll bar on the web page set on the right?

Why are the scroll bars of the browsers and word ...

Linux file/directory permissions and ownership management

1. Overview of file permissions and ownership 1. ...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Detailed explanation of box-sizing in CSS3 (content-box and border-box)

Box-sizing in CSS3 (content-box and border-box) T...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

Vue basics MVVM, template syntax and data binding

Table of contents 1. Vue Overview Vue official we...

How to make an input text box change length according to its content

First: Copy code The code is as follows: <input...

Detailed explanation of MySQL date addition and subtraction functions

1. addtime() Add the specified number of seconds ...