93 words
1 minute
Cumulative total over time in SQL
Anubhav Gain
2024-03-02

Cumulative total over time in SQL#

This is a quick trick for creating a cumulative chart of the total number of items created over time based just on their creation date.

select
created_at,
(
select
count(*)
from
repos repos2
where
repos2.owner = 9599
and repos2.created_at <= repos.created_at
) as cumulative
from
repos
where
"owner" = 9599
order by
created_at desc

I imagine there’s a more elegant way to do this using a window function but this works fine.

Cumulative total over time in SQL
https://mranv.pages.dev/posts/cumulative-total-over-time-in-sql/
Author
Anubhav Gain
Published at
2024-03-02
License
CC BY-NC-SA 4.0