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

条件聚合

中等 09-aggregate 整表聚合 aggregateconditionalfilter-clausecount-if

在一次扫描里统计已付款和已退款的订单,并计算退款率。

  • paid_cnt:status = 'paid' 的订单数。
  • refunded_cnt:status = 'refunded' 的订单数。没有退款订单时为 0(不是 null)。
  • refund_rate:refunded_cnt / (paid_cnt + refunded_cnt),浮点数,不做舍入。分母只算这两种状态,其他状态(cancelled、pending、null)不计入。
  • 题目保证分母大于 0。

输入

  • orders(order_id, customer_id, order_ts, status, channel):status 可能为 null

输出

恰好一行,列为 paid_cnt, refunded_cnt, refund_rate。

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

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,行顺序不限)

#paid_cntInt64refunded_cntInt64refund_rateFloat64
1 122 33 0.2129032258
共 1 行