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_id | attribute | value |
| 1 | age | 28 |
| 1 | height | 175 |
| 1 | weight | 70 |
| 2 | age | 34 |
| 2 | height | 180 |
| 2 | weight | 80 |
The goal is one column per attribute:
| user_id | age | height | weight |
| 1 | 28 | 175 | 70 |
| 2 | 34 | 180 | 80 |
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:
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.
| Technique | Description | Example Snippet |
| Aggregate Functions | Use maxIf with conditions to group and pivot | maxIf(value, attribute='age') AS age |
| Arrays | Utilize array functions for dynamic attributes | groupArray(attribute) AS attrs |
| Performance Tips | Enhance performance with indexing and distribution | Ensure 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.
Related reading
- Trouble connecting to postgres from outside Kubernetes cluster
- Troubleshooting Illegal mix of collations error in mysql
- Truncate all tables in a MySQL database in one command?
- Trying to setup Mongo replication, but end up with two secondary members and no primary
- TTL vs default_time_to_live which one is better and why?
- Two instances of application connected to same, altered database
- Two Phase Commit blocking on coordinator failure
- Two phase commit what happens if the coordinator dies between sending two confirmations
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.