段的DAU / MAU计算

时间:2018-12-28 19:10:22

标签: sql amazon-redshift

我编写了以下查询来计算DAU / MAU比率:

WITH dau AS
(
  SELECT TRUNC(created_at) AS created_at,
         CASE
           WHEN user_agent SIMILAR TO '%(Mobile|iPhone|iPod|iPad|Android)%' THEN 'non-desktop'
           ELSE 'desktop'
         END AS trafficsource,
         COUNT(DISTINCT member_id) AS dau
  FROM table ds
  WHERE ds.created_at BETWEEN '2017-01-01' AND '2017-12-11'
  AND   member_id <> 2
  AND   member_id NOT IN (SELECT memberid FROM auth2.membersinglerole WHERE roleid = 25)
  GROUP BY TRUNC(created_at),
           trafficsource
)
SELECT created_at,
       trafficsource,
       dau,
       (SELECT COUNT(DISTINCT member_id)
        FROM table ds
        WHERE member_id <> 2
        AND   member_id NOT IN (SELECT memberid FROM auth2.membersinglerole WHERE roleid = 25)
        AND   ds.created_at BETWEEN dau.created_at - 29*INTERVAL '1 day' AND dau.created_at) AS mau,
       (dau / CAST(mau AS float)) AS "DAU/MAU",
       (dau / CAST(mau AS float))*30 AS DaysOutOf30
FROM dau
WHERE EXTRACT(dayofweek FROM created_at) NOT IN (0,6)
AND   EXTRACT(month FROM created_at) NOT IN (5,6,7)
ORDER BY created_at

此查询为“桌面”和“非桌面”创建2个细分。但值得注意的是,查询在同一天为这两个细分返回相同的“ MAU”号,如下所示。

created_at    trafficsource    dau    mau    DAU/MAU    DaysOutOf30
2017-01-02  desktop 4157    140834  0.02951702003777497 0.8855106011332491
2017-01-02  non-desktop 801 140834  0.005687547041197438    0.17062641123592315
2017-01-03  desktop 12610   140468  0.089771335820258   2.6931400746077396
2017-01-03  non-desktop 2891    140468  0.020581199988609505    0.6174359996582851
2017-01-04  non-desktop 4033    137516  0.029327496436778264    0.8798248931033479
2017-01-04  desktop 17902   137516  0.1301812152767678  3.9054364583030337

如何修复查询以为创建的细分返回“ MAU”值?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

通过分别计算然后将其重新连接到mau以获得最终结果,可以获得准确的dau值。

逻辑步骤:

  1. 每天traffic每行创建1行member_id。请注意,该范围比您最终选择的范围早30天开始。
  2. 创建dau,该内容基本上保持不变
  3. 通过将mau与自身(使用区间逻辑)引入最近30天的数据的范围联接进行联接来创建traffic。在该数据上计算不同的member_id
  4. 选择最终数据,将dau上的maucreated_at连接起来。

WITH traffic AS (
    SELECT member_id,
           TRUNC(created_at) AS created_at,
           CASE WHEN user_agent SIMILAR TO '%(Mobile|iPhone|iPod|iPad|Android)%' 
           THEN 'non-desktop' ELSE 'desktop' END AS trafficsource,
    FROM table ds
    WHERE ds.created_at BETWEEN '2016-12-01' AND '2017-12-11'
    AND   member_id <> 2
    AND   member_id NOT IN (SELECT memberid FROM auth2.membersinglerole WHERE roleid = 25)
    GROUP BY 1,2,3
), dau AS (
    SELECT created_at, trafficsource,
           COUNT(DISTINCT member_id) AS dau
    FROM traffic
    GROUP BY 1,2
), mau AS (
    SELECT ds.created_at, ds.trafficsource,
           COUNT(DISTINCT ds.member_id) AS mau
    FROM traffic ds
    LEFT JOIN traffic mth
        ON  ds.member_id = mth.member_id
        AND ds.created_at BETWEEN mth.created_at - 29*INTERVAL '1 day' AND mth.created_at
    GROUP BY 1,2
    )
SELECT created_at, trafficsource, dau, mau,
       (dau / CAST(mau AS float)) AS "DAU/MAU",
       (dau / CAST(mau AS float))*30 AS DaysOutOf30
FROM dau
JOIN mau    
    USING (created_at, trafficsource)
WHERE created_at BETWEEN '2017-01-01' AND '2017-12-11'
AND   EXTRACT(dayofweek FROM created_at) NOT IN (0,6)
AND   EXTRACT(month FROM created_at) NOT IN (5,6,7)
ORDER BY created_at
;