0

尝试执行我的语句时,我不断收到错误,说如下:

ERROR [42703] [IBM][DB2/LINUXX8664] SQL0206N "HAO" is not valid in the context where it is used.

错误指的是我的过滤器,因为它在注释掉时可以正常工作。见下文:


SELECT
    ROUND(MINUTESDIFF(CURRENT_DATE,LAST_ACTION_DATE)/60,2) AS HAO
FROM [database]
WHERE   1=1
    AND QTY     >      0
    AND STATE   =      'Active'
    AND HAO     >      5

ORDER BY ONHOLD DESC

你不能使用像“HAO”这样的别名作为过滤器吗?我想我以前看过它,所以我一定做错了什么,请告诉我,谢谢!

4

1 回答 1

3

您不能在 DB2 中同时创建和使用字段(其他数据库确实允许这样做)。

你应该这样做:

SELECT HAO
FROM (
    SELECT
        ROUND(MINUTESDIFF(CURRENT_DATE,LAST_ACTION_DATE)/60,2) AS HAO,
        ONHOLD
    FROM [database]
    WHERE   1=1
        AND QTY     >      0
        AND STATE   =      'Active'
) TMP 
WHERE HAO     >      5
ORDER BY ONHOLD DESC

或者:

SELECT
    ROUND(MINUTESDIFF(CURRENT_DATE,LAST_ACTION_DATE)/60,2) AS HAO
FROM [database]
WHERE   1=1
    AND QTY     >      0
    AND STATE   =      'Active'
    AND ROUND(MINUTESDIFF(CURRENT_DATE,LAST_ACTION_DATE)/60,2) > 5
ORDER BY ONHOLD DESC
于 2020-03-06T13:36:37.413 回答