[英]How to calculate running sum for each group in MySQL
我可以用sum()
函数进行直接求和。 但我这里有不同的情况。 我有一个只有 2 个字段的表Student 。 例如,假设整个班级只有 1 个学生:
CREATE TABLE student
(`dateOfExam` date, score int)
;
INSERT INTO student
(`dateOfExam`, `score`)
VALUES
('2020-05-28',5),
('2020-05-29',5),
('2020-05-30',10),
('2020-06-03',10),
('2020-06-05',5),
('2020-07-21',20),
('2020-07-22',10),
('2020-07-28',10)
;
我有他参加考试的日子的分数,在运行时多列一栏,即举行考试的月份:
查询是(昨天从 stackoverflow 得到帮助):
select date_format(dateOfExam, '%Y-%m') ExamMonth
, dateOfExam
, score
from student;
结果:
+-----------+------------+-------+
| ExamMonth | dateOfExam | score |
+-----------+------------+-------+
| 2020-05 | 2020-05-28 | 5 |
| 2020-05 | 2020-05-29 | 5 |
| 2020-05 | 2020-05-30 | 10 |
| 2020-06 | 2020-06-03 | 10 |
| 2020-06 | 2020-06-05 | 5 |
| 2020-07 | 2020-07-21 | 20 |
| 2020-07 | 2020-07-22 | 10 |
| 2020-07 | 2020-07-28 | 10 |
+-----------+------------+-------+
我的要求是每个月都要奖励这个学生。 我会继续将他的得分对于每一日期为每个单独的一个月,累计得分总和达到10时和Reward2累积得分时,总和达到20所以决赛桌想这给他Reward1:
+---------------+---------------+-------+---------------+---------------+
| ExamMonth | dateOfExam | Score | Reward1 | Reward2 |
+---------------+---------------+-------+---------------+---------------+
| 2020-05 | 2020-05-28 | 5 | | |
| | 2020-05-29 | 5 | Y | |
| | 2020-05-30 | 10 | | Y |
|---------------|---------------|-------|---------------|---------------|
| 2020-06 | 2020-06-03 | 10 | Y | |
| | 2020-06-05 | 5 | | |
|---------------|---------------|-------|---------------|---------------|
| 2020-7 | 2020-07-21 | 20 | Y | Y |
| | 2020-07-22 | 10 | | |
| | 2020-07-28 | 10 | | |
+---------------+---------------+-------+---------------+---------------+
奖励字段可以是布尔值,空奖励行可以设置为 N 或 False 或任何看起来合乎逻辑的东西。 这没有帮助: 计算运行总和
请帮助我实现这个目标。 建议一些方法。
这是一个小提琴。
以下代码段按 ExamMonth 上的基本查询分组,然后在决定 Reward1 和 Reward2 的值时使用一个案例。 此查询仅供您参考。 请重写为最适合您的方式。
select DERIVED2.ExamMonth, CASE WHEN DERIVED2.Cumul_Score >= 10 THEN 'Y'
ELSE ''
END AS Rewards1,
CASE WHEN DERIVED2.Cumul_Score >= 20 THEN 'Y'
ELSE ''
END AS Rewards2
FROM
(
select DERIVED1.ExamMonth, SUM(DERIVED1.score) as Cumul_Score
FROM
(
select date_format(dateOfExam, '%Y-%m') ExamMonth,
dateOfExam, score
from student
order by dateOfExam
) DERIVED1
GROUP BY ExamMonth
) DERIVED2
首先计算 CTE 中每个月的分数的运行总和。
然后应用你的条件:
with cte as (
select date_format(dateOfExam, '%Y-%m') ExamMonth,
dateOfExam, score,
sum(score) over (partition by date_format(dateOfExam, '%Y-%m') order by dateOfExam) total
from student
)
select ExamMonth, dateOfExam, score,
case when sum(total >= 10) over (partition by ExamMonth order by dateOfExam) = 1 then 'Y' end Reward1,
case when sum(total >= 20) over (partition by ExamMonth order by dateOfExam) = 1 then 'Y' end Reward2
from cte
请参阅演示。
结果:
> ExamMonth | dateOfExam | score | Reward1 | Reward2
> :-------- | :--------- | ----: | :------ | :------
> 2020-05 | 2020-05-28 | 5 | null | null
> 2020-05 | 2020-05-29 | 5 | Y | null
> 2020-05 | 2020-05-30 | 10 | null | Y
> 2020-06 | 2020-06-03 | 10 | Y | null
> 2020-06 | 2020-06-05 | 5 | null | null
> 2020-07 | 2020-07-21 | 20 | Y | Y
> 2020-07 | 2020-07-22 | 10 | null | null
> 2020-07 | 2020-07-28 | 10 | null | null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.