跳转到主要内容

说明

If 组合器可应用于 argMax 函数,使用 argMaxIf 聚合组合器函数查找在条件为 true 的行中与 val 最大值对应的 arg 值。 当你需要在数据集中查找与最大值对应的值, 但仅限于满足特定 条件的行时,argMaxIf 函数非常有用。

示例用法

在本示例中,我们将使用一组产品销售示例数据来演示 argMaxIf 的用法。我们将找出价格最高的产品名称,但 仅限销量至少为 10 次的产品。
Query
CREATE TABLE product_sales
(
    product_name String,
    price Decimal32(2),
    sales_count UInt32
) ENGINE = Memory;

INSERT INTO product_sales VALUES
    ('Laptop', 999.99, 10),
    ('Phone', 499.99, 15),
    ('Tablet', 299.99, 0),
    ('Watch', 1199.99, 5),
    ('Headphones', 79.99, 20);

SELECT argMaxIf(product_name, price, sales_count >= 10) AS most_expensive_popular_product
FROM product_sales;
argMaxIf 函数将返回在所有至少售出 10 次的产品 (sales_count >= 10) 中, 价格最高的产品名称。 在这种情况下,它将返回 ‘Laptop’,因为在这些热门产品中, 它的价格最高 (999.99) 。
Response
   ┌─most_expensi⋯lar_product─┐
1. │ Laptop                   │
   └──────────────────────────┘

另请参阅

最后修改于 2026年6月10日