> ## 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.

# countIf

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

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

[`If`](/ja/reference/functions/aggregate-functions/combinators#-if) コンビネータは、[`count`](/ja/reference/functions/aggregate-functions/count)
関数に適用でき、条件が真である行の数を
`countIf` 集約コンビネータ関数を使って数えることができます。

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

この例では、ユーザーのログイン試行を保存するテーブルを作成し、
`countIf` を使って成功したログインの回数を数えます。

```sql title="Query" theme={null}
CREATE TABLE login_attempts(
    user_id UInt32,
    timestamp DateTime,
    is_successful UInt8
) ENGINE = MergeTree
ORDER BY ();

INSERT INTO login_attempts VALUES
    (1, '2024-01-01 10:00:00', 1),
    (1, '2024-01-01 10:05:00', 0),
    (1, '2024-01-01 10:10:00', 1),
    (2, '2024-01-01 11:00:00', 1),
    (2, '2024-01-01 11:05:00', 1),
    (2, '2024-01-01 11:10:00', 0);

SELECT
    user_id,
    countIf(is_successful = 1) AS successful_logins
FROM login_attempts
GROUP BY user_id;
```

`countIf` 関数は、各ユーザーについて、`is_successful = 1` である行のみをカウントします。

```response title="Response" theme={null}
   ┌─user_id─┬─successful_logins─┐
1. │       1 │                 2 │
2. │       2 │                 2 │
   └─────────┴───────────────────┘
```

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

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