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

小计与总计

困难 10-groupby 分组聚合 groupbyrollupsubtotalconcatgrouping-sets

按渠道和品类汇总销售额,并附上每个渠道的小计和全部的总计。

  • 每个订单行的销售额为 qty × unit_price(忽略折扣,不区分订单状态)。订单行通过 order_id 取订单的 channel,通过 product_id 取商品的 category。
  • channel 为 null 的订单记作渠道 'unknown',和其他渠道一样参与汇总,也有自己的小计行。
  • 输出三种行:
  • 明细行:每个 (channel, category) 组合一行,sales 为该组合的销售额;
  • 小计行:每个渠道一行,category 为 null,sales 为该渠道全部品类的销售额;
  • 总计行:一行,channel 和 category 都为 null,sales 为全部销售额。
  • 只输出有销售记录的组合。

输入

  • order_items(order_id, product_id, qty, unit_price, discount)
  • products(product_id, name, category, price, cost, launched_on)
  • orders(order_id, customer_id, order_ts, status, channel)

输出

列为 channel, category, sales,行顺序不限。

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

order_items shop/order_items
#order_idint64product_idint64qtyint64unit_pricefloat64discountfloat64
1 1001 22 3 298.8 null
2 1001 26 1 190.0 0.1
3 1002 6 2 254.2 0.0
4 1002 9 3 79.0 null
5 1002 19 5 305.1 0.2
6 1003 9 2 79.0 0.1
7 1003 28 2 47.4 0.0
8 1004 5 1 500.0 0.1
9 1005 3 1 89.0 0.0
10 1005 12 4 45.0 null
11 1006 13 1 19.9 0.1
12 1006 25 2 39.9 null
13 1007 27 1 12.5 null
14 1008 3 1 89.0 null
15 1008 16 1 29.9 null
16 1008 17 1 129.0 null
17 1009 2 1 49.9 0.0
18 1009 10 2 42.5 null
19 1009 20 1 84.5 0.05
20 1010 1 1 23.8 0.1
显示前 20 行,共 500 行
products shop/products
#product_idint64namestringcategorystringpricefloat64costfloat64launched_ondate
1 1 Wireless Mouse Electronics 25.0 16.77 2025-05-07
2 2 USB-C Hub Electronics 49.9 34.17 2025-07-27
3 3 Bluetooth Speaker Electronics 89.0 61.47 2025-02-10
4 4 Mechanical Keyboard Electronics 199.0 105.48 2024-09-19
5 5 4K Monitor Electronics 500.0 282.38 2024-07-10
6 6 Noise-cancel Headphones Electronics 299.0 211.57 2025-01-14
7 7 Python Cookbook Books 59.0 30.08 2025-09-15
8 8 SQL Pocket Guide Books 39.9 22.14 2025-01-20
9 9 数据分析实战 Books 79.0 40.83 2024-12-15
10 10 Clean Code Books 50.0 28.48 2025-03-13
11 11 统计学习方法 Books 99.0 70.22 2025-03-13
12 12 Desk Lamp Home 45.0 24.99 2025-11-04
13 13 Ceramic Mug Home 19.9 11.34 2025-12-02
14 14 Throw Pillow Home 35.0 19.23 2024-06-29
15 15 Wall Clock Home 99.0 47.98 2025-02-23
16 16 Storage Box Home 29.9 15.06 2025-02-27
17 17 Puzzle 1000pc Toys 129.0 59.53 2025-08-19
18 18 Building Blocks Toys 199.0 110.19 2025-04-18
19 19 RC Car Toys 359.0 208.55 2025-11-06
20 20 Plush Panda Toys 89.0 43.14 2026-01-12
显示前 20 行,共 30 行
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,行顺序不限)

#channelStringcategoryStringsalesFloat64
1 null null 118531.3
2 app null 50325.2
3 web null 36346.4
4 unknown null 13488.1
5 store null 11106.7
6 phone null 7264.9
7 unknown Electronics 7962.0
8 unknown Toys 1542.0
9 unknown Sports 1343.0
10 unknown Home 813.2
11 unknown Beauty 951.9
12 unknown Books 876.0
13 app Beauty 3364.7
14 app Toys 14768.3
15 web Beauty 2346.8
16 store Home 1278.1
17 web Sports 5565.0
18 store Beauty 953.7
19 web Toys 8301.0
20 app Books 4112.3
21 store Books 845.2
22 web Home 2647.1
23 store Toys 2764.1
24 store Sports 2166.3
25 app Sports 7941.0
26 web Books 3345.6
27 app Home 2192.1
28 app Electronics 17946.8
29 web Electronics 14140.9
30 phone Sports 215.0
31 phone Beauty 2199.9
32 phone Electronics 2144.0
33 phone Toys 1879.0
34 phone Home 132.5
35 store Electronics 3099.3
36 phone Books 694.5
共 36 行