Beräkna först den löpande summan av poängen för varje månad i en CTE.
Använd sedan dina villkor:
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
Se demon
.
Resultat:
> 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