arg_max / max_by 的 top-N
中等
24-duckdb-sql-only DuckDB SQL 专属
arg-maxmax-bytop-nlist
对每个品类,列出最贵的 3 个商品的名字。
top3 是字符串列表,按价格从高到低排列;价格相同时 product_id 小的在前。
price 为 null 的商品不参与排名。
- 品类里有价格的商品不足 3 个时,列表就只有那么多个元素。每个品类至少有一个有价格的商品。
考点:用“取最大值对应的 x”这类聚合函数的 top-N 形式,一次得到列表,不写窗口函数。判题只看结果。
输入
products(product_id, name, category, price, cost, launched_on)
输出
列为 category, top3,每个品类一行,行顺序不限;top3 内部的顺序要符合上面的要求。
输入表 (case base,共 2 个 case:base、edge)
期望输出 (case base,行顺序不限)
| # | categoryString | top3List(String) |
| 1 |
Electronics |
["4K Monitor", "Noise-cancel Headphones", "Mechanical Keyboard"] |
| 2 |
Books |
["统计学习方法", "数据分析实战", "Python Cookbook"] |
| 3 |
Home |
["Wall Clock", "Desk Lamp", "Throw Pillow"] |
| 4 |
Toys |
["RC Car", "Building Blocks", "Puzzle 1000pc"] |
| 5 |
Sports |
["Running Shoes", "Dumbbell Set", "Yoga Mat"] |
| 6 |
Beauty |
["Face Cream", "Sunscreen", "Shampoo"] |
共 6 行
参考答案 · arg_max / max_by 的 top-N