ClickHouse
data transformation
SQL query
rows to columns
database management

ClickHouse pivot: transpose rows to columns with maxIf

Codemia Knowledge Hub ·

ClickHouse has no native pivot table function, so you transpose rows to columns with conditional aggregation: one maxIf(value, attribute = '...') per output column, grouped by the row key. Array functions such as groupArray are another option. Transposing like this is common when the stored layout does not match the format you need for analysis or reporting.

Pivoting with maxIf and GROUP BY

Take a table user_data stored as attribute/value rows:

user_idattributevalue
1age28
1height175
1weight70
2age34
2height180
2weight80

The goal is one column per attribute:

user_idageheightweight
12817570
23418080
sql
1SELECT
2    user_id,
3    maxIf(value, attribute = 'age') AS age,
4    maxIf(value, attribute = 'height') AS height,
5    maxIf(value, attribute = 'weight') AS weight
6FROM user_data
7GROUP BY user_id

maxIf aggregates value only for rows where the condition holds, so each call produces one new column for one attribute, effectively turning every unique attribute value into its own column. GROUP BY user_id collects each user's rows into a single output row.

Using arrays and groupArray

Array functions are another approach, aimed at datasets with a dynamic set of columns:

sql
1SELECT
2    user_id,
3    groupArray(attribute) AS attrs,
4    groupArray(value) AS vals,
5    vals[indexOf(attrs, 'age')] AS age,
6    vals[indexOf(attrs, 'height')] AS height,
7    vals[indexOf(attrs, 'weight')] AS weight
8FROM user_data
9GROUP BY user_id

groupArray collects each user's attribute names and values into two parallel arrays, and indexOf looks up the position of each attribute. The attrs and vals arrays also hold every attribute a user has, even ones you did not list as columns. arrayJoin does the opposite job: it expands an array into rows, which is how you turn columns back into rows.

When conditional aggregation is not enough

Aggregate functions with conditional logic simulate pivot operations well for simple cases. For more complex pivot tables, consider external tools or integrations with ClickHouse.

TechniqueDescriptionExample Snippet
Aggregate FunctionsUse maxIf with conditions to group and pivotmaxIf(value, attribute='age') AS age
ArraysUtilize array functions for dynamic attributesgroupArray(attribute) AS attrs
Performance TipsEnhance performance with indexing and distributionEnsure even data distribution across shards

Performance tips for large pivots

  • Indexing: make sure columns used for filtering and grouping are indexed well.
  • Distribution: in distributed setups, spread data evenly across shards to avoid bottlenecks.
  • Batch processing: for large datasets, process in batches to stay within memory limits.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design