sum_if
Calculate revenue only from paid orders. A conditional aggregate admits rows whose condition is SQL true, so one query can calculate a segment without filtering every other aggregate to that same segment.
Prepare the input
The sample includes two paid orders and one pending order:
sql
CREATE TABLE orders(id UInt32, status String, amount Int64);
INSERT INTO orders VALUES (1, 'paid', 100), (2, 'paid', 60), (3, 'pending', 40);Calculate the segment
sql
SELECT sum_if(amount, status = 'paid') AS paid_result
FROM orders
FORMAT csv;text
160Selected NULL amounts are skipped. False and NULL conditions do not contribute.
Empty segments
count_if and sum_if return zero when no qualifying non-NULL value contributes. Arguments are evaluated before the condition is tested; an excluded row does not protect an argument expression from an evaluation error.
See aggregate combinators for the supported conditional functions and SQL FILTER alternatives.