# 数据探索器 — 月活跃会员占比

**URL:** <https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [2021年三月4日 13:11 UTC](https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022 "2021-03-04T13:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konrad\_Sopala](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/konrad_sopala/32/167014_2.png) [@Konrad\_Sopala](https://meta.discourse.org/u/Konrad_Sopala)\
**Post date:** [2021年三月4日 13:11 UTC](https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022/1 "2021-03-04T13:11:35Z")

</div>

大家好，欢迎使用和探索 [Data Explorer](https://meta.discourse.org/t/32566?silent=true) 插件！

感谢 @michebs 之前解答了所有问题。我还有一个关于 [Data Explorer](https://meta.discourse.org/t/32566?silent=true) 查询的问题。

是否有人创建过按月统计活跃用户百分比的查询？

“活跃”指的是用户在该月内对某些帖子进行了点赞或回复，次数不限，但至少需要有一次。百分比是基于社区所有成员总数计算的。例如，如果某月有两人参与互动：一人点赞了某个帖子，另一人进行了回复，那么活跃人数为 2，再将其除以社区成员总数即可得出百分比。

---

<div class="post-metadata">

**Author:** ![michebs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michebs/32/190102_2.png) [@michebs](https://meta.discourse.org/u/michebs)\
**Post date:** [2021年三月4日 17:22 UTC](https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022/2 "2021-03-04T17:22:31Z")

</div>

希望这能帮到您。  
需要说明的是，用户总数会随时间变化，因此查询中所考虑的是截至某项操作执行所在月份和年份的累计用户数。

```sql
WITH tt_users_by_month AS (
    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
        COUNT(*) AS "new_users_month"
    FROM users
    WHERE id > 0
    GROUP BY date_part('year', created_at), date_part('month', created_at)
    ORDER BY date_part('year', created_at) ASC, date_part('month', created_at)
),

total_users AS (
    SELECT
        year, 
        month, 
        SUM(new_users_month) over (ORDER BY year, month rows between unbounded preceding AND current row) AS total
    FROM tt_users_by_month ORDER BY year, month
)

SELECT 
    date_part('year', ua.created_at) AS year, 
    date_part('month', ua.created_at) AS month,
    TRUNC(COUNT(DISTINCT user_id)::decimal/tu.total*100,2) AS "%"
FROM 
    user_actions ua
INNER JOIN total_users tu ON (date_part('year', ua.created_at) = tu.year AND date_part('month', ua.created_at) = tu.month)
WHERE action_type IN (1,5)
	GROUP BY date_part('year', created_at), date_part('month', created_at), total

```

---

<div class="post-metadata">

**Author:** ![Konrad\_Sopala](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/konrad_sopala/32/167014_2.png) [@Konrad\_Sopala](https://meta.discourse.org/u/Konrad_Sopala)\
**Post date:** [2021年三月5日 08:03 UTC](https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022/3 "2021-03-05T08:03:23Z")

</div>

没错，完全合理！谢谢！

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2021年四月4日 08:03 UTC](https://meta.discourse.org/t/data-explorer-of-active-members-by-month/182022/4 "2021-04-04T08:03:44Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
