Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study on the principle and usage of mysql Cumulative aggregation

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This paper gives an example to describe the principle and usage of mysql cumulative polymerization. Share with you for your reference, the details are as follows:

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

SELECT a.empid, a.ordermonth.qty AS thismonth, SUM (b.qty) AS total, CAST (AVG (b.qty) AS DECIMAL (5L2)) AS avgFROM emporders aINNER JOIN emporders b ON a.empid=b.empid AND b.ordermonth

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report