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

交叉计数

中等 17-reshape 重塑 crosstabpivotcountfill-zero

统计每个国家、每种设备的注册用户数,做成交叉表。

  • 每个国家一行,设备列固定为 ios、android、web 三列,值为人数。
  • 没有用户的格子填 0;即使某种设备在整张表里一个用户都没有,这一列也要输出(全为 0)。
  • country 或 device 为 null 的用户不计入;只输出至少有一个计入用户的国家。

输入

  • users(user_id, signup_ts, country, device)

输出

列为 country, ios, android, web,行顺序、列顺序不限。

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

users web/users
#user_idint64signup_tsdatetimecountrystringdevicestring
1 1 2026-06-24 03:08:20 CN web
2 2 2026-06-20 22:19:20 DE web
3 3 2026-09-17 01:45:10 US android
4 4 2026-09-04 07:46:49 JP android
5 5 2026-07-17 14:10:25 CN ios
6 6 2026-07-26 18:38:11 JP android
7 7 2026-09-04 04:54:06 JP ios
8 8 2026-07-19 20:08:09 CN web
9 9 2026-08-09 06:02:40 JP web
10 10 2026-09-08 17:29:29 US android
11 11 2026-06-18 09:52:08 US ios
12 12 2026-08-28 19:51:40 CN android
13 13 2026-09-21 02:59:16 CN web
14 14 2026-08-11 14:56:25 CN android
15 15 2026-08-17 10:25:06 DE android
16 16 2026-06-23 14:41:12 US web
17 17 2026-09-04 08:22:08 CN android
18 18 2026-08-21 13:11:02 US web
19 19 2026-09-08 14:31:03 US ios
20 20 2026-07-14 14:21:42 JP web
显示前 20 行,共 60 行

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

#countryStringiosInt64androidInt64webInt64
1 JP 2 6 5
2 BR 2 3 2
3 CN 4 10 6
4 DE 2 1 1
5 US 5 8 3
共 5 行