如何修复“ ORA-00904:“ Y”:无效的标识符00904。00000-“%s:无效的标识符”“?

时间:2019-10-08 11:51:59

标签: sql oracle case

我试图创建一个使用变量输入的SQL select语句,以确定WHERE函数的运行方式。代码遇到了意外问题。

我尝试更改条件的语法,以便它们改用LIKE,但问题仍然存在。该变量肯定已保存,但仍然会显示错误。

SELECT (SELECT description FROM froomtype rt 
       WHERE rt.roomtypeid = rsd.roomtypeid),
       SUM(totalrevenue)
FROM froomservicedata rsd
WHERE (SELECT requestdate FROM froomservice rs WHERE rs.roomserviceid = rsd.roomserviceid) >
    (CASE
         WHEN UPPER(&&timeperiod) = 'D' THEN 
             sysdate - interval '24' hour  
         WHEN UPPER(&&timeperiod) = 'W' THEN 
             sysdate - interval '7' day  
         WHEN UPPER(&&timeperiod) = 'M' THEN 
             sysdate - interval '1' month   
         WHEN UPPER(&&timeperiod) = 'Y' THEN 
             sysdate - interval '1' year
         ELSE
            sysdate + 1
    END)
GROUP BY (SELECT description FROM froomtype rt WHERE rt.roomtypeid = rsd.roomtypeid);

这是错误消息:

ORA-00904: "Y": invalid identifier
00904. 00000 -  "%s: invalid identifier"

1 个答案:

答案 0 :(得分:2)

您缺少单引号。

您的查询应为:

SELECT (SELECT description FROM froomtype rt 
       WHERE rt.roomtypeid = rsd.roomtypeid),
       SUM(totalrevenue)
FROM froomservicedata rsd
WHERE (SELECT requestdate FROM froomservice rs WHERE rs.roomserviceid = rsd.roomserviceid) >
    (CASE
         WHEN UPPER('&&timeperiod') = 'D' THEN 
             sysdate - interval '24' hour  
         WHEN UPPER('&&timeperiod') = 'W' THEN 
             sysdate - interval '7' day  
         WHEN UPPER('&&timeperiod') = 'M' THEN 
             sysdate - interval '1' month   
         WHEN UPPER('&&timeperiod') = 'Y' THEN 
             sysdate - interval '1' year
         ELSE
            sysdate + 1
    END)
GROUP BY (SELECT description FROM froomtype rt WHERE rt.roomtypeid = rsd.roomtypeid);

干杯!