Amazon Redshift - lateral column alias reference
This syntax is not safe. In fact, merely inlining the code means that it does not even provide a performance advantage. It is only syntactic sugar.
Given that there are easy alternatives -- CTEs and subqueries -- I would just avoid this new "feature".
If there were a setting to turn this off, I would recommend using it.
Incidentally, many newcomers to SQL find this quite disconcerting. This purpose is to avoid ambiguity. What should the following query return?
select (a + 1) as b, b
from (select 1 as a, 0 as b) x;
The designers of SQL probably felt that the rules around resolving such situations are more complex than merely rewriting a subquery.
The one "database" that I know of that resolves this well is actually SAS proc SQL
. It introduced the calculated
keyword, so you can write:
select (a + 1) as b, calculated b, b
from (select 1 as a, 0 as b) x;
And this would return 2, 2, 0
.
In other words, I don't think Amazon put much thought into the implementation of this "feature".
How do I use lateral column alias reference properly in Amazon Redshift?
I assume that the problem is coming from the SELECT
in
THEN (SELECT split_part(contact_email, '@', 2))
Could you remove the SELECT? I don't think it's required.
Related Topics
SQL Server - Invalid Characters in Parameter Names
Format a Number with Commas But Without Decimals in SQL Server 2008 R2
SQL Server Convert Datetime into Another Timezone
SQL Query for Grouping the Results Based on Sequence
Postgresql Reusing Computation Result in Select Query
Find Most Common Elements in Array with a Group By
Gaps Between Primary Key Id in SQL Table
How to Turn Off Implicit Type Conversion in SQL Server
How - Create and Use Database Directly After Creation in SQL Server
What Is Wrong with This SQL Server Query Division Calculation
How to Execute Dynamic SQL in Teradata
How to Find If a Value Exists Within a Varray
Cte in from Clause of SQL Query
How to Add Sequenced Number Based on Sorted Value in Query in Access