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

三表连接

中等 11-join 连接 joinmulti-tableaggregateNone

统计每个客户已付款订单的实付总金额。

  • 只看 status = 'paid' 的订单。
  • 订单行的实付金额 = qty × unit_price × (1 − discount);discount 为 null 表示没有折扣,按 0 计算。
  • paid_total:该客户所有已付款订单的订单行实付金额之和,不做舍入。
  • 只输出客户表里存在、并且至少有一个已付款订单行的客户;没有已付款订单的客户不输出。

输入

  • customers(customer_id, name, email, city, signup_date, tier)
  • orders(order_id, customer_id, order_ts, status, channel)
  • order_items(order_id, product_id, qty, unit_price, discount)

输出

列为 customer_id, name, paid_total,行顺序不限。

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

customers shop/customers
#customer_idint64namestringemailstringcitystringsignup_datedatetierstring
1 1 Fang Wang fang.wang@shop.io Beijing 2025-03-13 silver
2 2 Dana Xu dana.xu@mail.cn null 2025-02-17 silver
3 3 Li Zhou li.zhou@example.com Guangzhou 2025-10-07 bronze
4 4 Ping Wang ping.wang@example.com null 2025-05-23 gold
5 5 Tom Zhang tom.zhang@example.com Shenzhen 2025-06-26 gold
6 6 王芳 user06@shop.io Hangzhou 2025-02-10 gold
7 7 Victor Xu victor.xu@example.com Shanghai 2025-12-05 bronze
8 8 Dana Chen dana.chen@example.com Hangzhou 2025-11-22 bronze
9 9 Uma Chen uma.chen@shop.io Guangzhou 2025-11-28 null
10 10 Ivy Zhou ivy.zhou@shop.io Hangzhou 2025-07-14 gold
11 11 Mia Smith mia.smith@mail.cn Shanghai 2025-04-28 gold
12 12 Xin Yang xin.yang@example.com Chengdu 2026-01-03 silver
13 13 Xin Smith xin.smith@mail.cn Beijing 2025-05-07 silver
14 14 Zoe Xu zoe.xu@mail.cn Beijing 2025-09-18 silver
15 15 Fang Liu fang.liu@shop.io Guangzhou 2025-08-05 bronze
16 16 杨磊 user16@mail.cn Shanghai 2025-12-15 silver
17 17 Ping Smith ping.smith@mail.cn null 2025-08-11 bronze
18 18 Oscar Zhou oscar.zhou@example.com Shanghai 2025-11-17 null
19 19 Kevin Liu kevin.liu@mail.cn Chengdu 2026-02-03 gold
20 20 Sara Wu sara.wu@example.com null 2025-07-05 gold
显示前 20 行,共 50 行
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 行
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 行

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

#customer_idInt64nameStringpaid_totalFloat64
1 2 Dana Xu 5890.455
2 4 Ping Wang 971.42
3 5 Tom Zhang 4377.45
4 6 王芳 3508.54
5 8 Dana Chen 650.82
6 9 Uma Chen 2263.445
7 10 Ivy Zhou 270.0
8 16 杨磊 2055.62
9 18 Oscar Zhou 7454.395
10 25 张伟 1561.39
11 26 Mia Xu 4776.265
12 31 Nina Zhang 1000.0
13 32 Wei Zhang 400.92
14 36 陈静 681.26
15 43 Rui Smith 72.0
16 44 Eric Lee 126.155
17 45 Oscar Wang 409.0
18 46 Grace Smith 279.31
19 47 Fang Zhang 435.55
20 49 Lin Smith 291.785
21 50 Hao Chen 1084.055
22 11 Mia Smith 1431.93
23 12 Xin Yang 2789.73
24 13 Xin Smith 135.0
25 17 Ping Smith 719.065
26 19 Kevin Liu 807.2
27 21 Rui Chen 1663.6
28 22 刘洋 2811.82
29 24 Kevin Lee 1072.27
30 27 Amy Huang 2341.885
31 28 Nina Wu 2605.2
32 29 Bob Smith 4378.21
33 34 Qing Zhao 873.51
34 38 Dana Zhao 966.02
35 39 Sara Chen 1314.1
36 40 Chen Wang 430.8
37 42 Ivy Wu 152.82
共 37 行