Alias referencing
Important Notes
Aliases defined in the SELECT clause cannot be referenced in the WHERE clause (this is due to SQL's logical processing order)
Aliases can be referenced in GROUP BY, HAVING, and ORDER BY clauses
For column aliases with spaces or special characters, use double quotes
Critical Update
When working with PostgreSQL, there is a known issue regarding aliases in inline SQL statements and datasets. Aliases written in any mixed or uppercase format (e.g., CamelCase, lowerCamelCase) are returned by the PostgreSQL driver as lowercase field names. Using these aliases in code (like DataRow["ColName"]) or as a Binding Path will lead to runtime errors or binding failures. To resolve this issue, consider the following change:

For cases where your V16 Statement references aliases in WHERE clause, below are 3 possible options of re-writing the SQL:
V16 SQL
V17 possible conversions
The fix includes creating a Common Table Expression (CTE) named dt with all selected columns.
The query will:
Filter that CTE for records where cf_me_bez contains 'buc'
Order the results by artnr from the CTE
The nested CTE approach can improve readability and might offer better performance in some database systems by breaking down the query into logical components.
This query will:
Create a first CTE (
art_with_mm_grid) that handles the join betweenartandmm_gridCreates a second CTE (
art_with_me_units) that adds the measurement unit description via a subqueryThe main query then filters and sorts the results
The fix includes converting the subquery in the SELECT clause to a LATERAL join. The LATERAL join allows us to reference columns from the left side ("art"."me_nr") in the right side subquery.
Fixed the WHERE clause to reference the actual subquery rather than the alias (PostgreSQL doesn't allow referencing column aliases in the WHERE clause)
Attention, we highly recommend not using this version.
The fix includes maintaining the structure by using a derived table (subquery) dt.
The query will correctly:
Create a derived table with all your selected columns
Filter that derived table for records where cf_me_bez contains 'buc'
Order the results by artnr
Key Differences Summarized
Return Value
Single value (1 row, 1 column)
Result set (multiple rows/columns)
Result set (multiple rows/columns) for each outer row
Placement
SELECT list, WHERE, HAVING, expressions
WITH clause at query start
FROM clause (with JOIN LATERAL)
Correlation
Can be correlated or non-correlated
Cannot directly reference outer query columns
Explicitly correlated to preceding tables
Reusability
No direct reuse of the subquery itself
Yes, can be referenced multiple times
No direct reuse of the LATERAL subquery's definition (it's tied to the JOIN)
Primary Goal
Get a single value for an expression/comparison
Modularize complex queries, enable recursion
Row-dependent set retrieval ("Top-N per group")
Analogy
A function call returning a single value
A named view for a single query
A function call that generates a table for each row
When to Use Each
CTE
Breaking down complex queries
Materialized separately
LATERAL Join
Row-wise joins with correlations
Optimized like a join
Scalar Subquery
Simple single-value lookups
Slow for large datasets
Last updated