在MySQL中,SUM
函数结合窗口函数 OVER
可以实现对结果集中的某个列进行累加求和。使用 SUM OVER
可以计算每一行与前面行的累积和。
01、 以下是 SUM OVER
的使用示例
SELECT column1, column2, SUM(column3) OVER (ORDER BY column1) AS cumulative_sum
FROM your_table;
在上面的查询中,your_table
是你的表名,column1
、column2
和 column3
分别是你要查询的列名。SUM(column3) OVER (ORDER BY column1)
表示计算 column3
列的累积和,按照 column1
列的升序进行排序。
示例使用:
假设有一个表 sales
,其中包含销售记录,包括销售日期(sale_date
)、销售金额(amount
) 和产品名称(product_name
)。我们想要计算每一天的销售累计金额
-- 示例数据
CREATE TABLE sales (
sale_date DATE,
amount DECIMAL(10, 2),
product_name VARCHAR(100)
);
INSERT INTO sales (sale_date, amount, product_name) VALUES
('2023-06-01', 100.00, 'Product A'),
('2023-06-01', 200.00, 'Product B'),
('2023-06-02', 150.00, 'Product A'),
('2023-06-02', 250.00, 'Product B'),
('2023-06-03', 120.00, 'Product A');
-- 计算每一天的销售累计金额
SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS cumulative_amount
FROM sales
ORDER BY sale_date;
输出结果:
+------------+--------+------------------+
| sale_date | amount | cumulative_amount|
+------------+--------+------------------+
| 2023-06-01 | 100.00 | 100.00 |
| 2023-06-01 | 200.00 | 300.00 |
| 2023-06-02 | 150.00 | 450.00 |
| 2023-06-02 | 250.00 | 700.00 |
| 2023-06-03 | 120.00 | 820.00 |
+------------+--------+------------------+
在上面的示例中,我们使用 SUM(amount) OVER (ORDER BY sale_date)
计算每一天的销售累计金额。结果集按照 sale_date
列的升序进行排序,并计算了每一行与前面行的累积和。
请注意,SUM OVER
结果将显示在每一行,而不是在整个结果集的末尾。这使得您可以在每一行上获取累积和的值。
在MySQL中,SUM
函数结合窗口函数 OVER
和 PARTITION BY
子句可以实现对每个分区内的某个列进行累加求和。使用 SUM OVER PARTITION BY
可以计算每个分区内的累积和。
02、以下是 SUM OVER PARTITION BY
的使用示例:
SELECT column1, column2, column3, SUM(column3) OVER (PARTITION BY column1 ORDER BY column2) AS cumulative_sum
FROM your_table;
在上面的查询中,your_table
是你的表名,column1
、column2
和 column3
分别是你要查询的列名。SUM(column3) OVER (PARTITION BY column1 ORDER BY column2)
表示计算 column3
列的累积和,按照 column1
列进行分区,并在每个分区内按照 column2
列的升序进行排序。
示例使用:
假设有一个表 sales
,其中包含销售记录,包括销售日期(sale_date
)、产品名称(product_name
) 和销售金额(amount
)。我们想要计算每个产品的销售累计金额,按照销售日期排序。
-- 示例数据
CREATE TABLE sales (
sale_date DATE,
product_name VARCHAR(100),
amount DECIMAL(10, 2)
);
INSERT INTO sales (sale_date, product_name, amount) VALUES
('2023-06-01', 'Product A', 100.00),
('2023-06-01', 'Product B', 200.00),
('2023-06-02', 'Product A', 150.00),
('2023-06-02', 'Product B', 250.00),
('2023-06-03', 'Product A', 120.00),
('2023-06-03', 'Product B', 180.00);
-- 计算每个产品的销售累计金额
SELECT sale_date, product_name, amount,
SUM(amount) OVER (PARTITION BY product_name ORDER BY sale_date) AS cumulative_amount
FROM sales
ORDER BY product_name, sale_date;
输出结果:
+------------+--------------+--------+---------------------+
| sale_date | product_name | amount | cumulative_amount |
+------------+--------------+--------+---------------------+
| 2023-06-01 | Product A | 100.00 | 100.00 |
| 2023-06-02 | Product A | 150.00 | 250.00 |
| 2023-06-03 | Product A | 120.00 | 370.00 |
| 2023-06-01 | Product B | 200.00 | 200.00 |
| 2023-06-02 | Product B | 250.00 | 450.00 |
| 2023-06-03 | Product B | 180.00 | 630.00 |
+------------+--------------+--------+---------------------+
在上面的示例中,我们使用 SUM(amount) OVER (PARTITION BY product_name ORDER BY sale_date)
计算每个产品的销售累计金额。