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

分组累计

简单 16-window-cumulative 窗口:累计与滑动 cumsumpartitionframetie-break

计算每个客户按时间顺序的累计消费额。

  • 只看 status = 'paid' 的订单;customer_id 为 null 的订单不输出。
  • 订单金额 amount = 该订单所有订单行的 qty * unit_price * (1 - discount) 之和,discount 为 null 按 0 算。
  • 每个客户的订单按 order_ts 从早到晚排序,order_ts 相同时按 order_id 从小到大排;cum_amount 是按这个顺序逐单累加到当前这一单(含)为止的金额。
  • 同一时刻的两单,cum_amount 也要不同:排在前面的那单不包含后一单的金额。

输入

  • orders(order_id, customer_id, order_ts, status, channel)
  • order_items(order_id, product_id, qty, unit_price, discount)

输出

列为 customer_id, order_id, order_ts, amount, cum_amount,每个已付款订单一行,行顺序不限。

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

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_idInt64order_idInt64order_tsDatetime(time_unit='us', time_zone=None)amountFloat64cum_amountFloat64
1 50 1106 2026-02-20 15:02:34 379.35 379.35
2 50 1115 2026-05-13 07:37:11 704.705 1084.055
3 42 1027 2026-05-22 08:52:21 45.09 45.09
4 42 1139 2026-08-30 02:51:19 107.73 152.82
5 5 1129 2025-10-02 18:02:37 987.18 987.18
6 5 1048 2025-12-14 00:07:09 67.55 1054.73
7 5 1120 2026-01-28 23:24:36 758.86 1813.59
8 5 1031 2026-02-14 10:30:00 219.4 2032.99
9 5 1034 2026-02-14 10:30:00 459.9 2492.89
10 5 1032 2026-02-14 13:30:00 798.7 3291.59
11 5 1033 2026-02-14 16:30:00 477.32 3768.91
12 5 1153 2026-02-15 11:13:43 456.1 4225.01
13 5 1030 2026-06-12 16:22:09 24.5 4249.51
14 5 1124 2026-06-23 23:16:24 127.94 4377.45
15 9 1122 2025-12-14 12:51:43 398.45 398.45
16 9 1038 2025-12-18 09:01:58 248.0 646.45
17 9 1132 2026-01-15 00:19:13 68.305 714.755
18 9 1123 2026-02-01 19:21:59 638.4 1353.155
19 9 1054 2026-02-28 23:59:59 567.92 1921.075
20 9 1052 2026-04-01 00:00:00 191.32 2112.395
21 9 1059 2026-06-14 04:09:36 151.05 2263.445
22 17 1097 2026-05-01 15:44:19 621.355 621.355
23 17 1006 2026-08-15 05:15:25 97.71 719.065
24 44 1049 2026-07-31 23:48:12 126.155 126.155
25 22 1179 2026-06-01 10:37:35 162.0 162.0
26 22 1109 2026-06-14 03:11:11 513.27 675.27
27 22 1046 2026-06-30 01:58:03 129.72 804.99
28 22 1099 2026-07-09 09:32:23 1057.73 1862.72
29 22 1127 2026-07-22 18:31:53 949.1 2811.82
30 31 1154 2025-12-15 11:11:18 1000.0 1000.0
31 32 1041 2026-04-22 15:13:08 400.92 400.92
32 11 1080 2025-09-28 02:53:58 212.705 212.705
33 11 1186 2025-12-05 00:30:26 231.205 443.91
34 11 1087 2026-02-06 16:10:12 735.22 1179.13
35 11 1178 2026-04-16 22:19:07 252.8 1431.93
36 16 1002 2026-01-17 14:07:00 1965.8 1965.8
37 16 1114 2026-07-11 09:15:35 89.82 2055.62
38 26 1085 2025-12-29 17:22:00 313.81 313.81
39 26 1043 2026-01-05 21:57:00 442.3 756.11
40 26 1098 2026-01-09 18:42:00 878.84 1634.95
41 26 1196 2026-01-19 10:02:00 605.61 2240.56
42 26 1144 2026-01-28 08:40:00 902.8 3143.36
43 26 1019 2026-02-05 15:02:26 297.1 3440.46
44 26 1095 2026-06-04 10:21:13 739.1 4179.56
45 26 1165 2026-08-19 19:00:32 368.705 4548.265
46 26 1181 2026-09-13 20:40:59 228.0 4776.265
47 6 1200 2025-08-15 18:15:29 1075.2 1075.2
48 6 1044 2026-02-09 18:36:53 1308.84 2384.04
49 6 1170 2026-08-29 20:25:45 1124.5 3508.54
50 38 1079 2026-07-21 21:45:24 649.02 649.02
显示前 50 行,共 121 行