Explore this snippet with some demo data here.
Running totals are relatively simple to calculate in BigQuery, using the sum window function.
The template for the query is
select
sum(<value>) over (partition by <fields> order by <ordering> asc) running_total,
<fields>,
<ordering>
from <table>where
value- this is the numeric quantity to calculate a running total forfields- these are zero or more columns to split the running totals byordering- this is the column which determines the order of the running total, most commonly temporaltable- where to pull these columns from
Using total Spotify streams as an example data source, let's identify:
value- this isstreamsfields- this is justartistordering- this is thedaycolumntable- this is calledraw
then the query is:
select
sum(streams) over (partition by artist order by day asc) running_total,
artist,
day
from raw| running_total | artist | day | streams |
|---|---|---|---|
| 705819 | *NSYNC | 2017-12-23 | 705819 |
| 2042673 | *NSYNC | 2017-12-24 | 1336854 |
| ... | ... | ... | ... |