Using Window functions for cumulatives

As a second example, consider the case where you're computing sales for each month orders were placed:

    SELECT EXTRACT(year FROM orderdate) AS year, EXTRACT(month FROM orderdate) AS month,sum(netamount) as sales FROM orders GROUP BY year,month ORDER BY year,month;
     year | month |   sales   
    ------+-------+-----------
     2004 |     1 | 199444.50
     2004 |     2 | 200269.86
     2004 |     3 | 194516.68
     2004 |     4 | 200131.18
     2004 |     5 | 197131.77
     2004 |     6 | 199919.21
     2004 |     7 | 190953.70
     2004 |     8 | 194102.55
     2004 |     9 | 196798.83
     2004 |    10 | 195798.80
     2004 |    11 | 199882.86
     2004 |    12 | 202769.80
  

What if you wanted a cumulative total at each point during the year? As that problem also requires referencing things that are ...

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.