how do i create a cumulative sum grouped by column?

You can use correlated subqueries:

select t.*,
       (select count(*)
        from table_of_interest t2
        where t2.update_at <= t.updated_at and t2.state_one = 1
       ) as cumulative_sum_one_1,
       (select count(*)
        from table_of_interest t2
        where t2.update_at <= t.updated_at and t2.state_one = 2
       ) as cumulative_sum_two_2
from table_of_interest t;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top