Oracle SQL查询 - 请帮助

时间:2012-08-16 08:46:29

标签: sql oracle

我正在组合一个查询,该查询在很多场合通过别名引用一个表,但它会进入状态:

* ORA-00904:“MPAN_STATUS”。“MPANCORE”:标识符无效 00904. 00000 - “%s:无效标识符” *原因:
动作: 行错误:43列:126

查询如下:

SELECT readings.mpancore, mtds.meterid, (SELECT CASE
                                                  WHEN meter_type 
                                                  LIKE 'RCAM%' AND
                                                       retrieval_method = 'R'
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.mtds
                                                 WHERE meter_removed IS NULL) "IS_SMART",
                                          (SELECT CASE 
                                                  WHEN flowversion = 'D0010'          AND
                                                       readings.filename IS NOT NULL  AND
                                                       readings.filedate >= mpan_status.ssd 
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.readings, edmgr.mpan_status
                                                 WHERE readings.mpancore = mpan_status.mpancore) "D0010_RECEIVED",
                                          (SELECT CASE 
                                                  WHEN effective_from_date >= mpan_status.ssd AND
                                                       dc_id = mpan_status.confirmed_dc_id          
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.D0019_reg, edmgr.mpan_status) "D0019_RECEIVED",
                                          (SELECT CASE 
                                                  WHEN file_date_time <= SYSDATE-409       
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.D0019_reg) "RF_READ",
                                          (SELECT CASE 
                                                  WHEN file_date_time <= SYSDATE-220       
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.D0019_reg) "R3_READ",
                                          (SELECT CASE 
                                                  WHEN file_date_time <= SYSDATE-118       
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.D0019_reg) "R2_READ",
                                          (SELECT CASE 
                                                  WHEN file_date_time <= SYSDATE-56        
                                                  THEN 'TRUE' 
                                                  ELSE 'FALSE' END
                                                  FROM edmgr.D0019_reg) "R1_READ"   
        FROM edmgr.mpan_status "mpan_status" LEFT JOIN edmgr.D0019_reg "D0019"   ON mpan_status.mpancore = D0019.metering_system_id
                                             LEFT JOIN edmgr.readings "readings" ON mpan_status.mpancore = readings.mpancore
                                             LEFT JOIN edmgr.mtds "mtds"         ON readings.mpancore    = mtds.mpancore 
                                                   AND D0019.metering_system_id                          = mtds.mpancore
                                                   AND mtds.meterid                                      = readings.meterid

我真的是一个完整的新手,所以非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

让我们开始定义ORA-00904 error

  

ORA-00904字符串:无效的标识符   原因:输入的列名称丢失或无效。

     

操作:输入有效的列名称。有效列名必须以字母开头,小于或等于30个字符,并且仅包含字母数字字符和特殊字符$,_和#。

     

如果它包含其他字符,则必须用双引号括起来。它可能不是一个保留字。

这也是一个区分大小写的问题(在FROM参数中将mpan_status放在引号中。)在Oracle中,只有当您将它们放在引号中时,列名称才区分大小写。

编辑:

这是一个建议我会编写查询(避免使用ANSI连接和子查询)。

SELECT  readings.mpancore,
        mtds.meterid,
        CASE  
            WHEN meter_type LIKE 'RCAM%' AND retrieval_method = 'R' THEN 'TRUE' 
            ELSE 'FALSE' 
        END AS "IS_SMART"
FROM    edmgr.mpan_status,
        edmgr.mtds,
        edmgr.D0019_reg,
        edmgr.readings
WHERE   mpan_status.mpancore = D0019_reg.metering_system_id
    AND mpan_status.mpancore = readings.mpancore
    AND readings.mpancore    = mtds.mpancore
    AND D0019_reg.metering_system_id = mtds.mpancore;