説明
If コンビネータは、any
集約関数に適用でき、指定した条件に一致する
指定のカラム内の最初の要素を選択します。
使用例
この例では、成功フラグ付きの売上データを格納するテーブルを作成し、
anyIf を使用して、金額が 200 を超えるものと下回るもののうち、最初の transaction_id を選択します。
まず、テーブルを作成し、そこにデータを insert します。
CREATE TABLE sales(
transaction_id UInt32,
amount Decimal(10,2),
is_successful UInt8
)
ENGINE = MergeTree()
ORDER BY tuple();
INSERT INTO sales VALUES
(1, 100.00, 1),
(2, 150.00, 1),
(3, 155.00, 0),
(4, 300.00, 1),
(5, 250.50, 0),
(6, 175.25, 1);SELECT
anyIf(transaction_id, amount < 200) AS tid_lt_200,
anyIf(transaction_id, amount > 200) AS tid_gt_200
FROM sales;┌─tid_lt_200─┬─tid_gt_200─┐
│ 1 │ 4 │
└────────────┴────────────┘