Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 4 min read How-to mediumAdept

SQL Dynamic Column Selection with ClickHouse

ClickHouse's COLUMNS clause selects columns by regular expression, so you can aggregate over a subset of a wide table without listing every column. Using the NYC yellow taxi dataset loaded into ClickHouse Local, the walkthrough shows how APPLY runs functions or lambdas across the matched columns, including chained aggregation and rounding. REPLACE swaps individual column expressions while leaving the rest unchanged, and EXCEPT drops columns from the match. Together these clauses cut down repetitive typing in analytical SQL.

Assumes: SQL basicsregular expressions

Related posts

  • ClickHouse Release 26.4

    ClickHouse mediumAdept

    ClickHouse 26.4 ships 39 features, 45 performance optimizations and 238 bug fixes, led by SQL-compatibility additions: VALUES as a table expression with column aliases, PostgreSQL-style EXTRACT units, SET TIME ZONE, NATURAL JOIN and compound INTERVAL literals. LIKE '%token%' queries can now scan the text index dictionary instead of the full column, which ran about 3x faster with far less memory on the HackerNews dataset. COUNT DISTINCT (uniqExact) merges all per-thread two-level hash tables in a single pass and stops spawning redundant threads, giving 3-15x speedups on a 288-core machine but nothing on a 12-core laptop. EXPLAIN pretty output is more readable, and a new JSONAllValues function lets a text index cover every leaf value of a JSON column.

  • Using Aggregate Combinators in ClickHouse

    ClickHouse mediumAdept

    ClickHouse aggregate combinators are suffixes appended to aggregate function names, and they can be chained, as in sumArrayIfOrNull. They change how an aggregation behaves without restructuring tables. If computes several conditional aggregates in one query, Distinct deduplicates inputs, and Resample buckets data into fixed-step groups, which arrayZip and arrayJoin can turn back into rows. OrDefault and OrNull control what empty result sets return, while Array and Map aggregate the contents of array and map columns, with worked SQL examples on sample payments and article-read tables.

  • Join Types supported in ClickHouse

    ClickHouse mediumAdept

    ClickHouse supports all standard SQL join types plus analytics-oriented variants: INNER, LEFT/RIGHT/FULL OUTER, CROSS, LEFT/RIGHT SEMI, LEFT/RIGHT ANTI, LEFT/RIGHT/INNER ANY, and ASOF joins. Each type is illustrated with Venn diagrams and runnable queries against a normalized IMDB dataset. The examples show ClickHouse-specific behaviour: non-matching rows get default values rather than NULLs unless configured otherwise. A CROSS JOIN with a WHERE join condition is rewritten into an ALL INNER JOIN, visible via EXPLAIN SYNTAX. ANY joins disable the cartesian product and return only the first match.

  • Querying Pandas DataFrames with ClickHouse

    ClickHouse mediumAdept

    chDB, an embedded Python library built on the ClickHouse engine, can run SQL directly against Pandas DataFrames through the chdb.dataframe module. DataFrames are passed as named parameters and referenced in SQL as __name__. The walkthrough uses Kaggle Canadian house-price data to show aggregation with GROUP BY ALL, joining two DataFrames, re-querying the resulting chDB Table via __table__, and converting results back with to_pandas(). It needs pandas and pyarrow installed, and string columns come back as bytes, so filters must compare against values like b"Ontario".

  • ClickHouse release 26.10

    ClickHouse mediumAdept

    ClickHouse 26.10 ships 104 features, 225 performance optimizations and 739 bug fixes. Headline additions include a `|` function composition operator with `_`/`_1` placeholders, which the analyzer rewrites into ordinary lambdas, and a `written_rows` quota for capping ingestion. Also new are array and map functions such as arrayMinIndex, arrayRemoveAt, mapRemove and mapContainsKeyValue, plus an opt-in `sql_compatible_division_for_integers` setting for PostgreSQL-style truncating division. IN subqueries can now spill their sets to disk via `max_bytes_before_external_set`, so a query that fails under a 512 MiB limit can complete. The release also brings faster GROUP BY, more flexible JOIN conditions and an experimental columns cache.

The quest for software engineering knowledge.

Look:
Layout: