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

自定义状态顺序

中等 03-sort 排序 sortcustom-orderenumcategorical

按业务上的状态顺序排列订单:pending → paid → refunded → cancelled(不是字母顺序)。

  • 先按上面的状态顺序排;同一状态内按 order_id 升序。
  • status 只会是这四个值之一或 null;status 为 null 的订单排在最后,彼此之间也按 order_id 升序(第二个 case 里有)。

输入

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

输出

列为 order_id, customer_id, status,行顺序必须与上面的排序规则一致。

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

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,行顺序要一致)

#order_idInt64customer_idInt64statusString
1 1017 9 pending
2 1037 38 pending
3 1056 5 pending
4 1089 998 pending
5 1093 9 pending
6 1102 19 pending
7 1103 9 pending
8 1104 50 pending
9 1113 45 pending
10 1138 31 pending
11 1156 2 pending
12 1171 3 pending
13 1189 5 pending
14 1195 12 pending
15 1001 39 paid
16 1002 16 paid
17 1004 2 paid
18 1005 12 paid
19 1006 17 paid
20 1008 45 paid
21 1009 2 paid
22 1010 4 paid
23 1012 2 paid
24 1014 2 paid
25 1018 999 paid
26 1019 26 paid
27 1024 47 paid
28 1026 18 paid
29 1027 42 paid
30 1028 36 paid
31 1029 18 paid
32 1030 5 paid
33 1031 5 paid
34 1032 5 paid
35 1033 5 paid
36 1034 5 paid
37 1038 9 paid
38 1039 2 paid
39 1041 32 paid
40 1043 26 paid
41 1044 6 paid
42 1046 22 paid
43 1047 8 paid
44 1048 5 paid
45 1049 44 paid
46 1051 12 paid
47 1052 9 paid
48 1053 12 paid
49 1054 9 paid
50 1057 2 paid
显示前 50 行,共 200 行