This article uses examples to illustrate the principle and usage of MySQL continuous aggregation. Share with you for your reference, the details are as follows: Continuous aggregation is the operation of aggregating ordered data in time order. In the following examples, the EmpOrders table is used to store the order quantity of each employee each month. Run the following code to create the EmpOrders table and populate it with sample data. CREATE TABLE EmpOrders ( empid INT NOT NULL, ordermonth DATE NOT NULL, qty INT NOT NULL,test PRIMARY KEY (empid,ordermonth) ); Query the order table and orderdetails table and insert the orders for each month into the EmpOrder table. The SQL statement is as follows (the trick is to group by month) INSERT INTO EmpOrders SELECT a.employeeid,orderdate AS Order date,SUM(quantity) AS qty FROM orders a INNER JOIN orderdetails b ON a.orderid=b.orderid GROUP BY employid,DATE_FORMAT(orderdate,'%Y-m'); The following is the PHP file that generates sample data <?php $sql = "INSERT INTO emporders SELECT %s,'%s-%02d-01',%s;".'<br />'; $insert_sql = ''; for($empid=1;$empid<=8;$empid++) { for($year=2009;$year<=2015;$year++) { for($month=1;$month<=12;$month++) { $num = rand(20,800); $insert_sql .= sprintf($sql,$empid,$year,$month,$num); } $insert_sql .= '<br />'; } } echo $insert_sql; The following is part of the data of the employee order table EmpOrder The following discusses three continuous aggregation issues based on the EmpOrders table: cumulative, sliding, and year-to-date. 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:
|
<<: jQuery implements HTML element hiding and display
>>: How to use Celery and Docker to handle periodic tasks in Django
1. Download 1. Click the latest download from the...
This article mainly introduces how to evenly dist...
1. First download from the official website of My...
The box model specifies the size of the element b...
Mind Map He probably looks like this: Most of the...
I have encountered many problems in learning Dock...
This article shares the specific code of js to ac...
When installing the centos7 version, choose to co...
Phenomenon The system could compile the Linux sys...
1. Monitoring planning Before creating a monitori...
Starting from IE 8, IE added a compatibility mode,...
This article mainly introduces the analysis of My...
I am going to review Java these two days, so I wr...
Table of contents Breaking down components Left P...
Step 1: Enter the directory: cd /etc/mysql, view ...