Reusing column aliases
In any database, you can use aliases for both columns and expressions within queries:
But what can we do with these aliases? Database vendors allow different things. For example, in MySQL and PostgreSQL, aliases can only be used in GROUP BY and ORDER BY. In Clickhouse and Snowflake, however, aliases can be used everywhere. But, as usual, there are nuances :)
Let's create syntactically identical VIEWs:
At first glance, the lineage for these views should be the same. But let's see what happened:
Why in V13 the column C not reuse the alias B? Because in Snowflake when using an alias equivalent to the original column name from the source, the original column takes precedence!
By the way, in Clickhouse, it works differently...
What was meant by saying that aliases work everywhere?
Aliases can be reused in JOIN and WHERE clauses:
or even like this:
Since in Dwh.dev we display not only the data flows but also the columns used in JOIN and WHERE, you will also see the original column sources in the corresponding section.
Last updated