struct 打包与 unnest
中等
23-polars-only Polars 专属
structunnestgroup-bytop-1
对每个品类,找出最贵的那个商品,并统计品类里的商品数。
- 最贵按
price 比较;price 为 null 的商品不能当选(但要计入商品数)。每个品类至少有一个价格不为 null 的商品。
- 价格并列时取
product_id 小的那个。
n_products 是该品类的商品行数(包括价格为 null 的)。
- 当选商品的
product_id、name、price 作为三个普通列输出。
考点:在一个聚合表达式里把多个字段打包成结构体一起取出来,最后再把结构体拆成普通列。判题只看结果。
输入
products(product_id, name, category, price, cost, launched_on)
输出
列为 category, product_id, name, price, n_products,每个品类一行,行顺序不限。
输入表 (case base,共 2 个 case:base、edge)
期望输出 (case base,行顺序不限)
| # | categoryString | product_idInt64 | nameString | priceFloat64 | n_productsUInt32 |
| 1 |
Home |
15 |
Wall Clock |
99.0 |
5 |
| 2 |
Beauty |
26 |
Face Cream |
200.0 |
5 |
| 3 |
Toys |
19 |
RC Car |
359.0 |
4 |
| 4 |
Sports |
23 |
Running Shoes |
699.0 |
5 |
| 5 |
Books |
11 |
统计学习方法 |
99.0 |
5 |
| 6 |
Electronics |
5 |
4K Monitor |
500.0 |
6 |
共 6 行
参考答案 · struct 打包与 unnest