df-drills pandas · Polars · DuckDB SQL · DuckDB Relational 题目列表 登录

日活与 7 日活跃

困难 19-timeseries 时间序列 daurolling-distinctself-joinexplode

计算每天的日活和 7 日活跃用户数。

  • 事件所在的“天”取 ts 的日期部分。
  • dau:当天有任意事件的不同用户数。
  • wau7:从当天往前数 7 天(含当天,即 [day - 6, day] 这 7 个日历日)内有任意事件的不同用户数。同一个用户在这 7 天里出现多次只算一次。
  • 只输出当天有事件的日期(没有事件的日期不输出,即使它的 7 日窗口内有活跃用户)。

输入

  • events(event_id, user_id, ts, event_type, page)

输出

列为 day, dau, wau7,day 为 date 类型,行顺序不限。

输入表 (case base,共 2 个 case:base、edge)

events web/events
#event_idint64user_idint64tsdatetimeevent_typestringpagestring
1 1 53 2026-06-03 17:57:33 login /home
2 2 53 2026-06-04 15:21:00 visit /home
3 3 53 2026-06-04 15:22:00 view /product/303
4 4 53 2026-06-04 15:32:04 add_to_cart /cart
5 5 56 2026-06-05 23:24:19 login /home
6 6 56 2026-06-05 23:29:18 view /product/202
7 7 56 2026-06-05 23:36:15 view /product/202
8 8 56 2026-06-05 23:47:36 add_to_cart /cart
9 9 56 2026-06-05 23:56:24 checkout /checkout
10 10 56 2026-06-06 19:14:00 visit /home
11 11 56 2026-06-06 19:22:53 view /product/303
12 12 56 2026-06-06 19:25:12 view /home
13 13 56 2026-06-06 19:28:25 add_to_cart /cart
14 14 29 2026-06-07 10:40:01 login /home
15 15 29 2026-06-07 10:46:43 view /product/303
16 16 29 2026-06-07 11:04:46 add_to_cart /cart
17 17 29 2026-06-07 11:09:25 checkout /checkout
18 18 34 2026-06-07 12:57:49 login /home
19 19 34 2026-06-07 13:03:57 view /product/202
20 20 34 2026-06-07 13:11:07 view /home
显示前 20 行,共 1500 行

期望输出 (case base,行顺序不限)

#dayDatedauInt64wau7Int64
1 2026-07-08 3 7
2 2026-07-14 1 8
3 2026-07-17 2 7
4 2026-07-23 3 9
5 2026-07-24 3 10
6 2026-07-28 2 12
7 2026-08-02 4 11
8 2026-08-11 3 9
9 2026-08-12 1 9
10 2026-08-16 2 7
11 2026-08-17 1 8
12 2026-08-18 3 8
13 2026-08-19 1 8
14 2026-08-22 1 11
15 2026-08-23 4 12
16 2026-08-24 4 14
17 2026-08-25 3 13
18 2026-08-26 2 13
19 2026-08-28 5 15
20 2026-08-29 5 16
21 2026-08-30 3 15
22 2026-09-01 1 14
23 2026-09-02 3 17
24 2026-09-08 7 24
25 2026-09-09 6 26
26 2026-09-17 5 27
27 2026-09-18 4 29
28 2026-09-19 6 25
29 2026-09-20 7 29
30 2026-09-29 7 19
31 2026-09-30 5 19
32 2026-10-01 4 21
33 2026-10-04 7 21
34 2026-10-06 3 18
35 2026-10-11 8 28
36 2026-10-12 4 29
37 2026-06-05 1 2
38 2026-06-06 1 2
39 2026-07-19 1 7
40 2026-07-29 2 12
41 2026-08-01 2 10
42 2026-08-14 1 8
43 2026-09-05 3 17
44 2026-09-23 4 29
45 2026-09-26 3 25
46 2026-10-15 1 25
47 2026-06-04 1 1
48 2026-06-10 1 4
49 2026-06-15 1 2
50 2026-06-17 1 2
显示前 50 行,共 117 行