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

# avgIf

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

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

[`If`](/ja/reference/functions/aggregate-functions/combinators#-if) コンビネータ は [`avg`](/ja/reference/functions/aggregate-functions/avg)
関数に適用でき、`avgIf` aggregate コンビネータ function を使用して、条件が true の行の値の算術平均を計算できます。

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

この例では、売上データと成功フラグを格納するテーブルを作成し、
成功した取引の平均売上額を計算するために `avgIf` を使用します。

```sql title="Query" theme={null}
CREATE TABLE sales(
    transaction_id UInt32,
    amount Decimal(10,2),
    is_successful UInt8
) ENGINE = MergeTree
ORDER BY ();

INSERT INTO sales VALUES
    (1, 100.50, 1),
    (2, 200.75, 1),
    (3, 150.25, 0),
    (4, 300.00, 1),
    (5, 250.50, 0),
    (6, 175.25, 1);

SELECT
    avgIf(amount, is_successful = 1) AS avg_successful_sale
FROM sales;
```

`avgIf` 関数は、`is_successful = 1` である行のみを対象に平均額を計算します。
この場合、100.50、200.75、300.00、175.25 の金額の平均を求めます。

```response title="Response" theme={null}
   ┌─avg_successful_sale─┐
1. │              193.88 │
   └─────────────────────┘
```

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

* [`avg`](/ja/reference/functions/aggregate-functions/avg)
* [`If コンビネータ`](/ja/reference/functions/aggregate-functions/combinators#-if)
