Mysql时间数据分段累加求和案例之子查询与SUM窗口函数
Posted ShenLiang2025
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql时间数据分段累加求和案例之子查询与SUM窗口函数相关的知识,希望对你有一定的参考价值。
mysql时间段数分段据累加求和
简介
时间段数据的分段可以根据义务逻辑来划分,累加求和在mysql8.0及以上可以通过sum结合窗口函数来实现,低版本用子查询实现。
需求
对原始数据按照时间点3点前、6点前、12点前对数值的累计得到如下结果:
时间段 累计
3 6
6 17
12 39原始数据附录如下:
id inserttime num
1 2020-07-30 00:00:00 1
2 2020-07-30 01:00:00 2
3 2020-07-30 02:00:00 3
4 2020-07-30 03:00:00 2
5 2020-07-30 04:00:00 3
6 2020-07-30 05:00:00 6
7 2020-07-30 06:00:00 1
8 2020-07-30 07:00:00 5
9 2020-07-30 08:00:00 7
10 2020-07-30 09:00:00 2
11 2020-07-30 10:00:00 4
12 2020-07-30 11:00:00 3
解决方法
-- Ⅰ关于问题,如果是Mysql 8.0用sum结合窗口函数,Mysql5.7及以下用子查询。
-- Ⅱ详细过程 Step By Step见代码处
-- Step 1 初始化表及数据
create table tb_shenliang2025(
id int,
inserttime datetime,
num int
);
INSERT INTO tb_shenliang2025
SELECT 1 id,'2020-07-30 00:00:00' inserttime,'1' num UNION ALL
SELECT 2 id,'2020-07-30 01:00:00' inserttime,'2' num UNION ALL
SELECT 3 id,'2020-07-30 02:00:00' inserttime,'3' num UNION ALL
SELECT 4 id,'2020-07-30 03:00:00' inserttime,'2' num UNION ALL
SELECT 5 id,'2020-07-30 04:00:00' inserttime,'3' num UNION ALL
SELECT 6 id,'2020-07-30 05:00:00' inserttime,'6' num UNION ALL
SELECT 7 id,'2020-07-30 06:00:00' inserttime,'1' num UNION ALL
SELECT 8 id,'2020-07-30 07:00:00' inserttime,'5' num UNION ALL
SELECT 9 id,'2020-07-30 08:00:00' inserttime,'7' num UNION ALL
SELECT 10 id,'2020-07-30 09:00:00' inserttime,'2' num UNION ALL
SELECT 11 id,'2020-07-30 10:00:00' inserttime,'4' num UNION ALL
SELECT 12 id,'2020-07-30 11:00:00' inserttime,'3' num
-- Step 2 Mysql8.0及以上结合窗口函数编写累计求和SQL
SELECT DISTINCT "时间段",
SUM(num)OVER(ORDER BY flag RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "累计"
--SUM(num)OVER(ORDER BY flag) AS "累计" --默认即上述参数
FROM
(
SELECT
CASE WHEN hour(inserttime)>=0 AND hour(inserttime)<3 THEN 3
WHEN hour(inserttime)>=3 AND hour(inserttime)<6 THEN 6
ELSE 12 END flag,
num FROM tb_shenliang2025 A
)A
-- Step 3 Mysql 5.7及以下版本(同样适用于Mysql8.0)
SELECT A.flag "时间段",(
SELECT SUM(num) FROM
(
SELECT flag,SUM(num) num FROM
(SELECT
CASE WHEN hour(inserttime)>=0 AND hour(inserttime)<3 THEN 3
WHEN hour(inserttime)>=3 AND hour(inserttime)<6 THEN 6
ELSE 12 END flag,num FROM tb_shenliang2025
)A
GROUP BY flag
)B
WHERE B.flag<=A.flag) "累计"
FROM
(
SELECT flag,SUM(num) num FROM
(SELECT
CASE WHEN hour(inserttime)>=0 AND hour(inserttime)<3 THEN 3
WHEN hour(inserttime)>=3 AND hour(inserttime)<6 THEN 6
ELSE 12 END flag,num FROM tb_shenliang2025
)A
GROUP BY flag
)A
-- Step 4 延展阅读,如果Step3的代码看上去麻烦,可以看如下简略版
-- 该辅助解释代码仅适用于Mysql8.0,因CTE在mysql低版本不支持
WITH CTE AS(
SELECT 1 id,6 num UNION ALL
SELECT 2 id,11 num UNION ALL
SELECT 3 id,22 num UNION ALL
SELECT 4 id,19 num
)
SELECT A.id,(
SELECT SUM(num) FROM CTE B
WHERE B.id<=A.id) total
FROM CTE A
-- 这里的CTE相当于Step3里按时间段分组统计。
SELECT flag,SUM(num) num FROM
(SELECT
CASE WHEN hour(inserttime)>=0 AND hour(inserttime)<3 THEN 3
WHEN hour(inserttime)>=3 AND hour(inserttime)<6 THEN 6
ELSE 12 END flag,num FROM tb_shenliang2025
)A
GROUP BY flag
-- 执行结果
以上是关于Mysql时间数据分段累加求和案例之子查询与SUM窗口函数的主要内容,如果未能解决你的问题,请参考以下文章