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)
期望输出 (case base,行顺序要一致)
| # | channelString | monthDate | statusString | n_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 行
参考答案 · GROUP BY ALL / ORDER BY ALL