段的 DAU/MAU 计算
Posted
技术标签:
【中文标题】段的 DAU/MAU 计算【英文标题】:DAU/MAU calculation for segments 【发布时间】:2018-12-28 19:10:21 【问题描述】:我编写了以下查询来计算 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”值?任何帮助将不胜感激。
【问题讨论】:
您能否给我们一些示例输入数据,以便我们尝试复制您的情况? 另外,您的mau
内部子选择从不区分桌面和非桌面,因此两者之间的 mau 保持不变。我猜你不是故意的,但是你应该添加那个?!
【参考方案1】:
您可以通过单独计算得到准确的mau
值,然后将其连接回dau
以获得最终结果。
逻辑步骤:
-
创建
traffic
,每天每个member_id
1 行。请注意,该范围比您的最终选择范围早 30 天开始。
创建dau
,它几乎没有变化
通过将traffic
连接到自身来创建mau
,并使用范围连接(使用您的间隔逻辑)引入过去30 天的数据。在该数据上计算不同的member_id
。
选择最终数据,在created_at
上加入dau
和mau
。
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
;
【讨论】:
以上是关于段的 DAU/MAU 计算的主要内容,如果未能解决你的问题,请参考以下文章
REDIS05_HyperLogLog的概述基本命令UVPVDAUMAU首页UV如何进行统计处理
REDIS05_HyperLogLog的概述基本命令UVPVDAUMAU首页UV如何进行统计处理