我可以用sum()函数进行简单的求和。但我这里的情况不一样。我有一个只有两个字段的桌子学生。例如,假设全班只有一个学生:
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)
;我得到了他考试的分数,在运行时再加上一栏,也就是举行考试的月份:
查询是(昨天从堆栈溢出处获得帮助):
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时给他Reward1,当累计分数达到20时给他Reward2。所以最后的表格应该是这样的:
+---------------+---------------+-------+---------------+---------------+
| 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或任何看似合乎逻辑的行。这是没有帮助的:计算运行和
请帮助我实现这一目标。建议一些方法。
这是一个小提琴。
发布于 2020-09-21 06:34:20
首先计算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 发布于 2020-09-21 05:06:18
下面的代码片段组由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
) DERIVED2https://stackoverflow.com/questions/63986019
复制相似问题