What is the principle of cumulative aggregation in MySQL database
What is the principle of cumulative aggregation in the MySQL database? many novices are not very clear about it. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Cumulative gathering
Cumulative aggregation is the aggregation of data from the first element in the sequence to the current element, such as returning the number of orders accumulated from the beginning of each month to now and the average number of orders for each employee.
There are two solutions to the line number problem, which are to use subqueries and to use joins. The method of subquery is usually intuitive and readable. However, when aggregation is required, the subquery needs to scan the data once for each aggregation, while the join method usually only needs to scan once to get the results.
The following query uses joins to get the results
SELECTa.empid,a.ordermonth,a.qty AS thismonth,SUM (b.qty) AS total,CAST (AVG (b.qty) AS DECIMAL (5pm 2)) AS avgFROM emporders an INNER JOIN emporders bON a.empid=b.empidAND b.ordermonth