Skip to content

Instantly share code, notes, and snippets.

@whbzju
Created June 20, 2017 05:35
Show Gist options
  • Save whbzju/a3cffa7f210f32050227c1f578979459 to your computer and use it in GitHub Desktop.
Save whbzju/a3cffa7f210f32050227c1f578979459 to your computer and use it in GitHub Desktop.
cumsum
select
a.id,
a.sum_all,
sum(a.sum_all) over (order by a.sum_all, a.id
rows between unbounded preceding
and current row)
as csum
from
( select col1 as id,
sum(col2) as sum_all
from t
group by col1
) a
order by
sum_all, id ;
select
col1 as id,
sum(col2) as sum_all,
sum(sum(col2)) over (order by sum(col2), col1
rows between unbounded preceding
and current row)
as csum
from
t
group by
col1
order by
sum_all, id ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment