count

Counts rows or non-NULL values.

Syntax

count()
count(<expr>)

Arguments

ArgumentTypeDescription
<expr>Any typeOptional input expression. Omit it to count every row; provide it to count non-NULL values.

Returns

Returns int. Returns 0 for empty input, or when count(<expr>) sees only NULL values.

Examples

Count all checkout events and those with a buyer ID:

VALUES
    ('web', 'buyer-17'), ('web', NULL), ('web', 'buyer-42'),
    ('mobile', 'buyer-63'), ('mobile', NULL)
SELECT $0 AS channel, $1 AS buyer_id
GROUP BY channel
AGGREGATE count() AS events, count(buyer_id) AS identified_events
ORDER BY channel
+---------+--------+-------------------+
| channel | events | identified_events |
+---------+--------+-------------------+
| mobile  | 2      | 1                 |
| web     | 3      | 2                 |
+---------+--------+-------------------+

count(buyer_id) counts non-NULL values, including repeated IDs. Use count_distinct to count distinct buyers.