Skip to content

Instantly share code, notes, and snippets.

@filipre
Created February 16, 2017 14:37
Show Gist options
  • Save filipre/3e1fc2a41933b74e9f5d04fe3b0c483d to your computer and use it in GitHub Desktop.
Save filipre/3e1fc2a41933b74e9f5d04fe3b0c483d to your computer and use it in GitHub Desktop.
-- Running sum partitioned by customer and month with some formatting
with months(n, name) as (
values (1, 'January'), (2, 'February'), (3, 'March'), (4, 'April'), (5, 'May'), (6, 'June'), (7, 'July'), (8, 'August'), (9, 'September'), (10, 'October'), (11, 'November'), (12, 'December')
)
select c_name, (select name from months where n = extract(month from o_orderdate)) as month, o_orderdate, sum(o_totalprice) over (
partition by o_custkey, extract(month from o_orderdate)
order by o_orderdate
) / 1000000.0 || ' Mio'
from orders, customer
where customer.c_custkey = orders.o_custkey
order by o_custkey, o_orderdate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment