Is there a way where we could query using aliases
SELECT
'X' AS AVC
FROM
sysibm.sysdummy1
WHERE AVC= 'X'
I am just looking for a way , I could query for alias column.
I am getting the below error.
Error: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=AVC,
DRIVER=3.63.75
SQLState: 42703
ErrorCode: -206
Error: DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURLH200C1,
DRIVER=3.63.75
SQLState: 26501
ErrorCode: -514
Any input would be helpful !
Thanks !
you can't use alias in where condition because the sql engine eval the query clause (FROM, WHERE, SELECT...) in a specific order and the select clause in evaluated after the where clause
so at the moment of the where evalution the column alias in not know by th db engine
You must repeat the code
SELECT 'X' AS AVC
FROM sysibm.sysdummy1
WHERE X' = 'X'
You can't. This is a general rule of SQL. In practice, you can think of the reason being that the where
is parsed before the select
, so the aliases are not known.
The general solution is to use a subquery or CTE:
SELECT t.*
FROM (SELECT 'X' AS AVC
FROM sysibm.sysdummy1
) t
WHERE AVC = 'X';
My preferred solution is to use a lateral join, but I don't think DB2 supports either APPLY
or LATERAL
.
The engine does not know of table or column aliases until it is finished gathering the data. You can use them in ORDER BY
statement within the same query but that's it. You can however, use a Common Table Expression
also known as a CTE
:
WITH myCTE AS (
SELECT 'X' AS AVC
FROM sysibm.sysdummy1
)
SELECT *
FROM myCTE
WHERE AVC= 'X'
This is somewhat of a long way to do it but unfortunately that is it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.