我正在尝试在 where 子句中使用列的别名。例如:
SELECT col1 AS alias1, col2 + col3 as sums
FROM my_table
WHERE sums > 10
但后来我收到一条错误消息:
列总和不在指定表中。
无论如何我可以做到这一点吗?
我正在尝试在 where 子句中使用列的别名。例如:
SELECT col1 AS alias1, col2 + col3 as sums
FROM my_table
WHERE sums > 10
但后来我收到一条错误消息:
列总和不在指定表中。
无论如何我可以做到这一点吗?
如果您真的想在中使用别名WHERE
,而不是列本身,则可以使用派生表:
SELECT a.*
FROM
(
SELECT lmITNO AS Item_Number, lmBANO AS Lot, lmSTAS AS Status,
lmRORN AS Order_Number
FROM MVXJDTA.MILOMA
) a
WHERE a.Status = 'CCC'
....
The where-clause is processed before the select-clause in a statement:
The WHERE clause specifies an intermediate result table that consists of those rows of R for which the search-condition is true. R is the result of the FROM clause of the statement.
Re-write the where-clause to reference the actual column name:
...
WHERE A.lmSTAS = 'CCC'
...
A common-table-expression can be used to pre-process the select-clause. For example:
WITH A AS (SELECT
lmITNO AS Item_Number,
lmBANO AS Lot,
lmSTAS AS Status,
lmRORN AS Order_Number
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY
The columns in a CTE can also be renamed by listing them after the table-identifier. For example:
WITH A (Item_Number, Lot, Status, Order_Number)
AS (SELECT
lmITNO,
lmBANO,
lmSTAS,
lmRORN
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY