[Input: sakila.payment]

[Problem]
[Output]

[Answer]
with base as (
select date(payment_date) as dt
,sum(amount) as daily_amount
from payment
where 1=1
and date_format(payment_date, "%Y%m") in ('200505', '200506')
group by date(payment_date)
)
select dt
,daily_amount
,sum(daily_amount) over (partition by month(dt) order by dt asc rows unbounded preceding) as accu_daily_amount
,case when count(1) over (partition by month(dt) order by dt asc rows between 6 preceding and current row) >= 7
then avg(daily_amount) over (partition by month(dt) order by dt asc rows between 6 preceding and current row)
else null end as seven_days_moving_avg
from base
order by dt asc;
[누적]
[이동평균]