Fix the MAU problem while calculating DAU and MAU on Amazon Redshift

∥☆過路亽.° 提交于 2020-01-04 06:25:26

问题


I am using the following query to calculate MAU and DAU, according to this post:

WITH dau AS
(
  SELECT TRUNC(created_at) AS created_at,
         COUNT(DISTINCT member_id) AS dau
  FROM table ds
  WHERE ds.created_at BETWEEN '2018-09-03' AND '2018-09-08'
  GROUP BY TRUNC(created_at)
)
SELECT created_at,
       dau,
       (SELECT COUNT(DISTINCT member_id)
        FROM table ds
        WHERE ds.created_at BETWEEN created_at - 29*INTERVAL '1 day' AND created_at) AS mau
FROM dau
ORDER BY created_at

I try running this query and get the following results:

2018-09-03  12844   3976132
2018-09-04  54236   3976132
2018-09-05  58631   3976132
2018-09-06  59786   3976132
2018-09-07  52317   3976132
2018-09-08  4   3976132

It can be clearly seen that the MAU column has repeating values. How do I fix this? Any pointers would be helpful.


回答1:


You should prefix column names:

WITH dau AS
(
  SELECT TRUNC(created_at) AS created_at,
         COUNT(DISTINCT member_id) AS dau
  FROM table ds
  WHERE ds.created_at BETWEEN '2018-09-03' AND '2018-09-08'
  GROUP BY TRUNC(created_at)
)
SELECT created_at,
       dau,
       (SELECT COUNT(DISTINCT member_id)
        FROM table ds
        WHERE ds.created_at 
          BETWEEN dau.created_at - 29*INTERVAL '1 day' AND dau.created_at) AS mau
          -- here
FROM dau
ORDER BY created_at

or:

SELECT TRUNC(created_at) AS created_at,
     COUNT(DISTINCT member_id) AS dau,
     COUNT(DISTINCT member_id) 
     FILTER(WHERE TRUNC(created_at)>=TRUNC(created_at)-29*INTERVAL '1 day') AS mau
FROM table ds
WHERE ds.created_at BETWEEN '2018-09-03' AND '2018-09-08'
GROUP BY TRUNC(created_at)
ORDER BY created_at


来源:https://stackoverflow.com/questions/53839230/fix-the-mau-problem-while-calculating-dau-and-mau-on-amazon-redshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!