Skip to content
Advertisement

Calculating the cumulative sum with a specific ‘date’ merged to single column in PostgreSQL

I have a database which contains the amounts and dates per user paid. Now some users make payments on the same day and I want to show the cumulative sum of these payments only once per day in a pivot table, which I am creating using Amazon QuickSight.

I have gone through the following, but they provide the cumulative values once per row and I don’t have a way to partition on just the date and not on anything else, with the sum over the payment made.

Calculating Cumulative Sum in PostgreSQL

Calculating cumulative sum with date filtering in PostgreSQL

Calculating Cumulative daily sum in PostgreSQL

PostgreSQL, renumber and cumulative sum at once

How to conditional sum two columns in PostgreSQL 9.3

My query looks like this:

Where I am doing a Sum() Over() at this row:

My Table has data as:

Users:

Payments:

The output I want is that the “Daily Amount” is shown only once for a specific date, if there are multiple rows with the same date, then for the other rows, it should be blank or display something like “NA”:

Is there some way that it is possible to get this output from SQL (PostgreSQL specific query)?

Advertisement

Answer

Looks like your sum() over() computes the wrong amount, try

EDIT If you want to format output (cumulative amount only once per date), use row_number() to detect first row in a group. Make sure over() clause is in sync with ORDER BY of the query.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement