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

GROUP BY ALL / ORDER BY ALL

简单 24-duckdb-sql-only DuckDB SQL 专属 group-by-allorder-by-allfriendly-sql

按 (channel, month, status) 统计订单数。

  • month 是 order_ts 所在月份的第一天,类型为日期(例如 2026-03-01)。
  • channel 为 null 的订单单独成一组(channel 输出 null)。
  • n_orders 是该组的订单行数。

排序(ordered: true):依次按 channel、month、status、n_orders 升序,null 排在最后。(前三列已经能唯一确定一行。)

考点:分组键和排序键都不手写列名,用“按所有非聚合列分组”“按所有列排序”的写法。判题只看结果。

输入

  • orders(order_id, customer_id, order_ts, status, channel)

输出

列为 channel, month, status, n_orders,按上面的顺序排列。

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

orders shop/orders
#order_idint64customer_idint64order_tsdatetimestatusstringchannelstring
1 1001 39 2026-01-01 19:38:00 paid app
2 1002 16 2026-01-17 14:07:00 paid app
3 1003 4 2026-01-08 15:40:20 cancelled web
4 1004 2 2026-05-23 10:48:58 paid null
5 1005 12 2026-06-22 11:07:22 paid store
6 1006 17 2026-08-15 05:15:25 paid web
7 1007 9 2026-06-11 01:00:32 refunded store
8 1008 45 2026-09-19 20:47:10 paid web
9 1009 2 2025-10-05 04:53:38 paid app
10 1010 4 2026-02-19 01:39:54 paid app
11 1011 28 2026-03-05 03:04:45 refunded store
12 1012 2 2026-08-01 07:09:17 paid web
13 1013 26 2026-01-22 03:48:14 refunded store
14 1014 2 2026-02-09 01:41:41 paid app
15 1015 5 2026-08-03 19:49:04 refunded web
16 1016 26 2026-02-16 00:43:43 refunded app
17 1017 9 2025-12-06 04:48:57 pending app
18 1018 999 2026-05-19 12:13:43 paid store
19 1019 26 2026-02-05 15:02:26 paid store
20 1020 44 2025-11-24 11:19:00 cancelled web
显示前 20 行,共 200 行

期望输出 (case base,行顺序要一致)

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