0

I'm relatively new to using SQL in Apache Superset and I'm not sure where to look or how to solve my problem.

The short version of what I am trying to do is add a column of cumulative sum based on the total number of users by month.

Here is my PostgreSQL query so far:

SELECT 
    DATE(DATE_TRUNC('month', crdate)) AS "Month", 
    COUNT(DISTINCT user_id) AS "COUNT_DISTINCT(user_id)"
FROM 
    datasource 
WHERE 
    user_id IS NOT NULL
GROUP BY  
    DATE(DATE_TRUNC('month', create))
ORDER BY 
    "COUNT_DISTINCT(user_id)" DESC

Sum of Users by Month

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • https://stackoverflow.com/questions/22841206/calculating-cumulative-sum-in-postgresql – DPH Jul 11 '22 at 15:51

2 Answers2

0

There are some syntax errors, you can't order by an alias and in group by your date column is wrong, so it should be like this:

SELECT 
    DATE(DATE_TRUNC('month', crdate)) AS "Month", 
    COUNT(DISTINCT user_id) AS "COUNT_DISTINCT(user_id)"
FROM 
    datasource 
WHERE 
    user_id IS NOT NULL
GROUP BY  
    DATE(DATE_TRUNC('month', crdate)) AS "Month"
ORDER BY 
    COUNT_DISTINCT(user_id) desc
Pepe N O
  • 1,678
  • 1
  • 7
  • 11
0

You can use your query a Basis for the Window function

CREATE TABLE datasource(crdate timestamp,user_id int)
WITH CTE AS (
SELECT
     DATE_TRUNC('month',"crdate") as "Month",
    COUNT(DISTINCT user_id) AS "COUNT_DISTINCT(user_id)"
FROM 
    datasource 
WHERE 
    user_id IS NOT NULL
GROUP BY  
    DATE_TRUNC('month', "crdate")
)
SELECT "Month", SUM("COUNT_DISTINCT(user_id)") OVER (ORDER BY "Month") as cumultatove_sum
FROM CTE
Month | cumultatove_sum
:---- | --------------:

db<>fiddle here

nbk
  • 45,398
  • 8
  • 30
  • 47