The simplest form for using Window Functions to calculate a cumulative total in SQL is:

SELECT sum(value) OVER () AS cumulative_total FROM datasource

The example above will calculate a cumulative total on the field value for all records in the table datasource. This is probably oversimplified for most cases.

If the datasource table has data for multiple structures, stations, wells, etc, the PARTITION BY clause can be added to the OVER parameters to isolate the cumulative total calculations for each entity:

SELECT station_ndx, sum(value) OVER (PARTITION BY station_ndx) AS cumulative_total 
FROM datasource ORDER BY station_ndx

There will also probably be a time series component that needs to be considered so that totals accumulate chronologically. The ORDER BY clause can be added to the OVER parameters to accomplish this:

SELECT station_ndx, timestamp, 
sum(value) OVER (PARTITION BY station_ndx ORDER BY timestamp) AS cumulative_total 
FROM datasource ORDER BY station_ndx, timestamp

This last example will calculate a cumulative total over time (timestamp) for each station_ndx in the datasource. The ORDER BY partition at the end just assures that the final query output orders logically both by the station and the timestamp.

Finally, there is often a need to "reset" the cumulative total over the full period of record, for instance to calculate a cumulative annual total that returns to zero at the beginning of each year. Our example can be modified to reset the cumulative total at the beginning of each year by adding the year to the PARTITION BY clause, as follows:

SELECT station_ndx, timestamp, 
sum(value) OVER (PARTITION BY station_ndx, date_part('year',timestamp)
								 ORDER BY timestamp) AS cumulative_total 
FROM datasource 
ORDER BY station_ndx, timestamp