ClickHouse Functions/Aggregate Functions

sum

Sum numeric values — revenue totals, event counts, and metric roll-ups.

Signature

sum(column): Number | sumIf(column, condition): Number

Returns

Same numeric type as input (Int64/Float64/Decimal)

What it does

Returns the sum of all non-NULL values in a numeric column. sumIf(col, cond) sums only rows matching a condition — equivalent to SUM(CASE WHEN cond THEN col END) but faster.

sum() is the core revenue and metric aggregation function in ClickHouse. Combined with GROUP BY date buckets it produces time-series revenue charts. sumIf() performs conditional sums in a single pass — useful for multi-dimension pivot queries. For aggregating arrays of numbers, use arraySum() instead.

Notes

  • Overflow handling: UInt64 sums overflow silently. Cast to Int128 or Decimal for large monetary sums.
  • sumIf(col, cond) runs in one pass — never use SUM(CASE WHEN …) in ClickHouse.
  • For Decimal columns, sum() preserves precision without floating-point errors.

Example SQL

sum in ClickHouse SQL

sum-example.sql

TypeScript with hypequery

Use sum in a typed TypeScript query

hypequery gives you a type-safe query builder for ClickHouse. The generated schema maps your ClickHouse columns to TypeScript types, and raw SQL expressions let you incorporate functions like sum when you need them inside a builder query.

daily-revenue.ts

Common questions

What developers search for with sum

ClickHouse revenue aggregation TypeScript

sum(amount) with GROUP BY toStartOfDay gives daily revenue. In hypequery, combine with .groupBy() and .orderBy() for sorted time series.

sumIf ClickHouse conditional sum

sumIf(amount, status = 'paid') vs sumIf(amount, status = 'refunded') in a single query — one table scan for both totals.

FAQ

Frequently asked questions about sum

Does ClickHouse sum() handle NULLs?

Yes — NULL values are ignored in sum(). sum() returns 0 (not NULL) when all values are NULL.

Related functions

Functions used alongside sum

Next step

Use sum in a type-safe TypeScript query

hypequery generates TypeScript types from your ClickHouse schema. Use sum alongside the builder, and reach for raw SQL expressions when the function is not exposed as a dedicated helper.