开发者

Oracle date compare( invalid identifier)

I have this query:

SELECT TO_CHAR(HON.CREATE_DT,'DD-MM-YYYY') as CREATE_DT, 
       HON.ACCOUNT_NO,(SELECT TO_CHAR(CURRENT_DATE - INTERVAL '7' DAY, 'DD-MM-YYYY') FROM DUAL) AS NV
              FROM medium.HONSCRIBERS HON, 
                   medium.HONSCRIBER_TEXT pcode, 
                   medium.ACCESS_NUMBERS accnr
              WHERE 
                accnr.STATUS_CD = 'CU' AND
                pcode.TRADING_AS ='7777777'AND
                HON.ACCOUNT_NO=pcode.ACCOUNT_NO AND
                HON.ACCOUNT_NO=accnr.ACCOUNT_NO 

It returns :

[0] => Array
        (
            [CREATE_DT] => 28-02-2008
            [ACCOUNT_NO] => 20340322
            [NV] => 20-05-2011
        )

    [1] => Array
        (
            [CREATE_DT] => 28-02-2008
            [ACCOUNT_NO] => 20340322
            [NV] => 20-05-2011
        )

I need to compare dates CREATE_DT AND NV My query:

SELECT TO_CHAR(HON.CREATE_DT,'DD-MM-YYYY') as CREATE_DT, 
       HON.ACCOUNT_NO,(SELECT TO_CHAR(CURRENT_DATE - INTERVAL '7' DAY, 'DD-MM-YYY开发者_开发技巧Y') FROM DUAL) AS NV
              FROM medium.HONSCRIBERS HON, 
                   medium.HONSCRIBER_TEXT pcode, 
                   medium.ACCESS_NUMBERS accnr
              WHERE 
                accnr.STATUS_CD = 'CU' AND          CREATE_DT < NV AND
                pcode.TRADING_AS ='7777777'AND
                HON.ACCOUNT_NO=pcode.ACCOUNT_NO AND
                HON.ACCOUNT_NO=accnr.ACCOUNT_NO

I have this error:

[code] => 904
 [message] => ORA-00904: "NV": invalid identifier
 [offset] => 325

Why he always wrote me : "NV": invalid identifier? Thanks for help.


You can't use a column alias in the WHERE clause of the same query. You have to repeat the expression it is an alias for:

AND CREATE_DT < (CURRENT_DATE - INTERVAL '7' DAY)

(Note you don't want the TO_CHAR here otherwise the comparison will not work correctly)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜