I have two columns: wash score (wash_sc
) and agric score (agric_sc
). I want to sum both cumulatively and group by month.
Currently am able to accomplish this for one variable, though it is takes a long time to return the response:
select date_format(s1.createdAt,'%b-%Y') as month,
(
select AVG(s2.wash_sc)
from checkins s2
where s2.createdAt <= last_day(s1.createdAt)
) as total
from checkins s1
group by month
order by s1.createdAt;
How can I do this (faster!) for both columns?
2
Answers
You can do it using the window function
AVG()
: