> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-home-button.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# argMinIf

> argMinIfコンビネータの使用例

<div id="description">
  ## 説明
</div>

[`If`](/ja/reference/functions/aggregate-functions/combinators#-if) コンビネータ は [`argMin`](/ja/reference/functions/aggregate-functions/argMin)
関数に適用でき、条件が真である行の中から `val` の最小値に対応する `arg` の値を求める `argMinIf` aggregate コンビネータ function として使用されます。

`argMinIf` 関数は、データセット内の最小値に対応する値を求めたいが、それを特定の
条件を満たす行に限定したい場合に便利です。

<div id="example-usage">
  ## 使用例
</div>

この例では、製品の価格とそのタイムスタンプを保存するテーブルを作成し、
在庫がある各製品について `argMinIf` を使用して最安値を求めます。

```sql title="Query" theme={null}
CREATE TABLE product_prices(
    product_id UInt32,
    price Decimal(10,2),
    timestamp DateTime,
    in_stock UInt8
) ENGINE = MergeTree
ORDER BY ();

INSERT INTO product_prices VALUES
    (1, 10.99, '2024-01-01 10:00:00', 1),
    (1, 9.99, '2024-01-01 10:05:00', 1),
    (1, 11.99, '2024-01-01 10:10:00', 0),
    (2, 20.99, '2024-01-01 11:00:00', 1),
    (2, 19.99, '2024-01-01 11:05:00', 1),
    (2, 21.99, '2024-01-01 11:10:00', 1);

SELECT
    product_id,
    argMinIf(price, timestamp, in_stock = 1) AS lowest_price_when_in_stock
FROM product_prices
GROUP BY product_id;
```

`argMinIf` 関数は、各商品について最も早い `timestamp` に対応する価格を返します。
ただし、対象となるのは `in_stock = 1` である行のみです。例:

* 商品 1: 在庫がある行の中では、最も早い `timestamp` (10:00:00) に対応する価格は 10.99 です
* 商品 2: 在庫がある行の中では、最も早い `timestamp` (11:00:00) に対応する価格は 20.99 です

```response title="Response" theme={null}
   ┌─product_id─┬─lowest_price_when_in_stock─┐
1. │          1 │                      10.99 │
2. │          2 │                      20.99 │
   └────────────┴────────────────────────────┘
```

<div id="see-also">
  ## 関連項目
</div>

* [`argMin`](/ja/reference/functions/aggregate-functions/argMin)
* [`argMax`](/ja/reference/functions/aggregate-functions/argMax)
* [`argMaxIf`](/ja/guides/clickhouse/examples/aggregate-function-combinators/argMaxIf)
* [`If コンビネータ`](/ja/reference/functions/aggregate-functions/combinators#-if)
