Home > front end >  BigQuery/ SQL cumulative sum with group by date
BigQuery/ SQL cumulative sum with group by date

Time:10-18

I have a score which I classify as bad, normal or good. So I wrote a Query which shows me each class count grouped by the day.

date        bad   normal    good
01.10       2     4         2
02.10       null  4         3
03.10       1     null      5
04.10       3     1         1
05.10       5     1         null

But I need the cumulative sum up to each date. Like this

date        bad   normal    good
01.10       2     4         2
02.10       2     8         5
03.10       3     8         10
04.10       6     9         11
05.10       11    10        11

How do I do this?

CodePudding user response:

Consider below approach

select date, 
  sum(sum(bad)) over win as bad,
  sum(sum(normal)) over win as normal,
  sum(sum(good)) over win as good
from your_table
group by date
window win as (order by date)     

if applied to your case - output is

enter image description here

  • Related