Oracle子查询CASE无法正常工作

时间:2014-09-30 00:59:58

标签: sql oracle subquery case

我有一个非常长的子查询,我不会发布,除非我能确定问题实际上在其中,但它本身运行得非常好。但是,当我查询它时,我得到了ORA-00923: FROM keyword not found where expected

编辑:我不认为人们得到它。所以我把整个事情都放进去了。

    SELECT  Nvl(INV_INVESTMENTS.NAME, '') PROJECT_NAME,
        Nvl(RAGRPTS.SR_PERIOD_START, '') REPORTING_PERIOD_START,
        Nvl(RAGRPTS.SR_PERIOD_FINISH, '') REPORTING_PERIOD_FINISH,
        Nvl(INV_INVESTMENTS.CODE, '') PROJECT_ID,
        Nvl(ODF_CA_INV.NPI_RCV_CC, Nvl(ODF_CA_INV.NPI_RCV_ORDER, Nvl(ODF_CA_INV.NPI_RCV_REAL_ESTATE, Nvl(ODF_CA_INV.NPI_RCV_WBSCODE, '')))) CHARGECODE,
        Nvl(PROJECT_STAGE.NAME, '') PROJECT_PHASE,
        Nvl(INV_PROJECTS.PRSPONSOREDBY, '') PROJECT_SPONSOR,
        Nvl(To_Char(INV_INVESTMENTS.SCHEDULE_FINISH),'DD/MM/YY') PLANNED_COMPLETION_DATE,
        Nvl(ODF_CA_PROJECT.PM_READ_ONLY, '') PROJECT_MANAGER,   -- may not work
        -- Overall Project Completion placeholder
        -- Next Fortnightly Steerting Committee Meeting Date placeholder
        Nvl(RAGRPTS.OVERALL_STATUS, ''),
        Nvl(RAGRPTS.OVERALL_TREND, ''),
        Nvl(RAGRPTS.OVERALL_HEALTH_COMMENT, '') OVERALL_STATUS_COMMENT,
        Nvl(RAGRPTS.SCHEDULE_STATUS, ''),
        Nvl(RAGRPTS.SCHEDULE_TREND, ''),
        Nvl(RAGRPTS.SCHEDULE_EXPLANATION, ''),
        Nvl(RAGRPTS.SCOPE_STATUS, ''),
        Nvl(RAGRPTS.SCOPE_TREND, ''),
        Nvl(RAGRPTS.SCOPE_EXPLANATION, ''),
        Nvl(RAGRPTS.COST_STATUS, ''),
        Nvl(RAGRPTS.COST_TREND, ''),
        Nvl(RAGRPTS.COST_EXPLANATION, ''),
        Nvl(RAGRPTS.RESOURCE_STATUS, ''),
        Nvl(RAGRPTS.RESOURCE_TREND, ''),
        Nvl(RAGRPTS.RESOURCE_EXPLANATION, ''),
        Nvl(RAGRPTS.KEY_ACCOMPLISHMENTS, '') PROGRESS_COMPLETED,
        Nvl(RAGRPTS.UPCOMING_ACTIVITIES, '') TARGETS PLANNED

FROM    INV_INVESTMENTS,
        ODF_CA_INV,
        INV_PROJECTS,
        ODF_CA_PROJECT,
        CMN_LOOKUPS_V PROJECT_STAGE,
        (
            SELECT CODE,
                   ID,
                   SR_PERIOD_START,
                   SR_PERIOD_FINISH,
                   NAME,
                   OVERALL_HEALTH_COMMENT,
                   NEW_OVERALL_STATUS AS OVERALL_STATUS,
                   (CASE
                                WHEN NEW_OVERALL_STATUS = 'G' AND (OLD_OVERALL_STATUS = 'A' OR OLD_OVERALL_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_OVERALL_STATUS = 'A' AND (OLD_OVERALL_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_OVERALL_STATUS = 'G' AND (OLD_OVERALL_STATUS = 'G') THEN 'NO CHANGE'
                                WHEN NEW_OVERALL_STATUS = 'A' AND (OLD_OVERALL_STATUS = 'A') THEN 'NO CHANGE'
                                WHEN NEW_OVERALL_STATUS = 'R' AND (OLD_OVERALL_STATUS = 'R') THEN 'NO CHANGE'
                                WHEN NEW_OVERALL_STATUS = 'R' AND (OLD_OVERALL_STATUS = 'A' OR OLD_OVERALL_STATUS = 'G') THEN 'NEGATIVE'
                                WHEN NEW_OVERALL_STATUS = 'A' AND (OLD_OVERALL_STATUS = 'G') THEN 'NEGATIVE'
                                ELSE 'N/A'
                    END) OVERALL_TREND,
                    SCHEDULE_EXPLANATION,
                    NEW_SCHED_STATUS AS SCHEDULE_STATUS,
                    (CASE
                                WHEN NEW_SCHED_STATUS = 'G' AND (OLD_SCHED_STATUS = 'A' OR OLD_SCHED_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_SCHED_STATUS = 'A' AND (OLD_SCHED_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_SCHED_STATUS = 'G' AND (OLD_SCHED_STATUS = 'G') THEN 'NO CHANGE'
                                WHEN NEW_SCHED_STATUS = 'A' AND (OLD_SCHED_STATUS = 'A') THEN 'NO CHANGE'
                                WHEN NEW_SCHED_STATUS = 'R' AND (OLD_SCHED_STATUS = 'R') THEN 'NO CHANGE'
                                WHEN NEW_SCHED_STATUS = 'R' AND (OLD_SCHED_STATUS = 'A' OR OLD_SCHED_STATUS = 'G') THEN 'NEGATIVE'
                                WHEN NEW_SCHED_STATUS = 'A' AND (OLD_SCHED_STATUS = 'G') THEN 'NEGATIVE'
                                ELSE 'N/A'
                    END) SCHEDULE_TREND,
                    SCOPE_EXPLANATION,
                    NEW_SCOPE_STATUS AS SCOPE_STATUS,
                    (CASE
                                WHEN NEW_SCOPE_STATUS = 'G' AND (OLD_SCOPE_STATUS = 'A' OR OLD_SCOPE_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_SCOPE_STATUS = 'A' AND (OLD_SCOPE_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_SCOPE_STATUS = 'G' AND (OLD_SCOPE_STATUS = 'G') THEN 'NO CHANGE'
                                WHEN NEW_SCOPE_STATUS = 'A' AND (OLD_SCOPE_STATUS = 'A') THEN 'NO CHANGE'
                                WHEN NEW_SCOPE_STATUS = 'R' AND (OLD_SCOPE_STATUS = 'R') THEN 'NO CHANGE'
                                WHEN NEW_SCOPE_STATUS = 'R' AND (OLD_SCOPE_STATUS = 'A' OR OLD_SCOPE_STATUS = 'G') THEN 'NEGATIVE'
                                WHEN NEW_SCOPE_STATUS = 'A' AND (OLD_SCOPE_STATUS = 'G') THEN 'NEGATIVE'
                                ELSE 'N/A'
                    END) SCOPE_TREND,
                    COST_EXPLANATION,
                    NEW_COST_STATUS AS COST_STATUS,
                    (CASE
                                WHEN NEW_COST_STATUS = 'G' AND (OLD_COST_STATUS = 'A' OR OLD_COST_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_COST_STATUS = 'A' AND (OLD_COST_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_COST_STATUS = 'G' AND (OLD_COST_STATUS = 'G') THEN 'NO CHANGE'
                                WHEN NEW_COST_STATUS = 'A' AND (OLD_COST_STATUS = 'A') THEN 'NO CHANGE'
                                WHEN NEW_COST_STATUS = 'R' AND (OLD_COST_STATUS = 'R') THEN 'NO CHANGE'
                                WHEN NEW_COST_STATUS = 'R' AND (OLD_COST_STATUS = 'A' OR OLD_COST_STATUS = 'G') THEN 'NEGATIVE'
                                WHEN NEW_COST_STATUS = 'A' AND (OLD_COST_STATUS = 'G') THEN 'NEGATIVE'
                                ELSE 'N/A'
                    END) COST_TREND,
                    RESOURCE_EXPLANATION,
                    NEW_RESOURCE_STATUS AS RESOURCE_STATUS,
                    (CASE
                                WHEN NEW_RESOURCE_STATUS = 'G' AND (OLD_RESOURCE_STATUS = 'A' OR OLD_RESOURCE_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_RESOURCE_STATUS = 'A' AND (OLD_RESOURCE_STATUS = 'R') THEN 'IMPROVED'
                                WHEN NEW_RESOURCE_STATUS = 'G' AND (OLD_RESOURCE_STATUS = 'G') THEN 'NO CHANGE'
                                WHEN NEW_RESOURCE_STATUS = 'A' AND (OLD_RESOURCE_STATUS = 'A') THEN 'NO CHANGE'
                                WHEN NEW_RESOURCE_STATUS = 'R' AND (OLD_RESOURCE_STATUS = 'R') THEN 'NO CHANGE'
                                WHEN NEW_RESOURCE_STATUS = 'R' AND (OLD_RESOURCE_STATUS = 'A' OR OLD_RESOURCE_STATUS = 'G') THEN 'NEGATIVE'
                                WHEN NEW_RESOURCE_STATUS = 'A' AND (OLD_RESOURCE_STATUS = 'G') THEN 'NEGATIVE'
                                ELSE 'N/A'
                    END) RESOURCE_TREND,
                    KEY_ACCOMPLISHMENTS,
                    UPCOMING_ACTIVITIES

            FROM (
                    SELECT  t1.CODE,
                            t1.ID,
                            SubStr(To_Char((Trunc(t1.CREATED_DATE, 'DD') - 7)), 1, 10) SR_PERIOD_START, 
                            SubStr(To_Char(t1.CREATED_DATE), 1, 10) SR_PERIOD_FINISH,
                            t1.NAME,
                            t1.OVERALL_HEALTH_COMMENT,
                            t1.OVERALL_STATUS AS NEW_OVERALL_STATUS,
                            t2.OVERALL_STATUS AS OLD_OVERALL_STATUS,
                            t1.SCHEDULE_EXPLANATION,
                            t1.SCHEDULE_STATUS AS NEW_SCHED_STATUS,
                            t2.SCHEDULE_STATUS AS OLD_SCHED_STATUS,
                            t1.SCOPE_EXPLANATION,
                            t1.SCOPE_STATUS AS NEW_SCOPE_STATUS,
                            t2.SCOPE_STATUS AS OLD_SCOPE_STATUS,
                            t1.COST_EXPLANATION,
                            t1.COST_STATUS AS NEW_COST_STATUS,
                            t2.COST_STATUS AS OLD_COST_STATUS,
                            t1.RESOURCE_EXPLANATION,
                            t1.RESOURCE_STATUS AS NEW_RESOURCE_STATUS,
                            t2.RESOURCE_STATUS AS OLD_RESOURCE_STATUS,
                            t1.KEY_ACCOMPLISHMENTS,
                            t1.UPCOMING_ACTIVITIES
                    FROM  (
                            SELECT  PRJ.CODE, 
                                    PRJ.ID,
                                    (SRM.LAST_NAME + ', ' + SRM.FIRST_NAME) FULL_NAME, 
                                    RAGRPT.CREATED_DATE, 
                                    RAGRPT.NAME,
                                    RAGRPT.COP_REPORT_UPDATE OVERALL_HEALTH_COMMENT,
                                    (CASE
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 40 AND 50 THEN 'G'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 60 AND 70 THEN 'A'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 80 AND 120 THEN 'R'
                                          ELSE ' '
                                    END) OVERALL_STATUS,
                                    RAGRPT.COP_SCHEDULE_EXP SCHEDULE_EXPLANATION,
                                    (CASE 
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) SCHEDULE_STATUS,
                                    RAGRPT.COP_SCOPE_EXP SCOPE_EXPLANATION,
                                    (CASE 
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) SCOPE_STATUS,
                                    RAGRPT.COP_EFFORT_EXP COST_EXPLANATION,
                                    (CASE 
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) COST_STATUS,
                                    RAGRPT.RMS_RESOURCE_EXP RESOURCE_EXPLANATION,
                                    (CASE 
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) RESOURCE_STATUS,
                                    RAGRPT.COP_KEY_ACCOMPLISH KEY_ACCOMPLISHMENTS,
                                    RAGRPT.COP_UPCOMING_ACT UPCOMING_ACTIVITIES,
                                    Row_Number() OVER (PARTITION BY PRJ.CODE ORDER BY RAGRPT.CREATED_DATE DESC) AS SEQNUM

                            FROM    ODF_CA_COP_PRJ_STATUSRPT RAGRPT, 
                                    INV_INVESTMENTS PRJ,
                                    SRM_RESOURCES SRM  
                            WHERE   PRJ.ID = RAGRPT.ODF_PARENT_ID (+)
                                    AND RAGRPT.CREATED_BY = SRM.ID (+)
                                    AND RAGRPT.CREATED_DATE BETWEEN Trunc(SYSDATE, 'DD') - 28 AND SYSDATE
                          )t1

                          LEFT JOIN

                          (
                            SELECT         
                                    (CASE
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 40 AND 50 THEN 'G'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 60 AND 70 THEN 'A'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 80 AND 120 THEN 'R'
                                          ELSE ' '
                                    END) OVERALL_STATUS,
                                    (CASE 
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_SCHEDULE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) SCHEDULE_STATUS,
                                    (CASE 
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_SCOPE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) SCOPE_STATUS,
                                    (CASE 
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.COP_COST_EFT_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) COST_STATUS,
                                    (CASE 
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 10 THEN 'G'
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 20 THEN 'A'
                                          WHEN RAGRPT.RMS_RESOURCE_STATUS = 30 THEN 'R'
                                          ELSE ' '
                                    END) RESOURCE_STATUS,
                                    PRJ.CODE,
                                    Row_Number() OVER (PARTITION BY PRJ.CODE ORDER BY RAGRPT.CREATED_DATE DESC) AS SEQNUM

                            FROM    ODF_CA_COP_PRJ_STATUSRPT RAGRPT, 
                                    INV_INVESTMENTS PRJ,
                                    SRM_RESOURCES SRM  
                            WHERE   PRJ.ID = RAGRPT.ODF_PARENT_ID (+)
                                    AND RAGRPT.CREATED_BY = SRM.ID (+)
                                    AND RAGRPT.CREATED_DATE BETWEEN Trunc(SYSDATE, 'DD') - 28 AND SYSDATE
                          )t2 

                          ON t1.code = t2.code
                    WHERE t1.SEQNUM = 1 
                          AND t2.SEQNUM = 2
                 )
            ) RAGRPTS

WHERE   INV_INVESTMENTS.ID = ODF_CA_PROJECT.ID
        AND INV_INVESTMENTS.ID = RAGRPTS.CODE (+)
        AND INV_INVESTMENTS.ID = ODF_CA_INV.ID
        AND INV_INVESTMENTS.ID = INV_PROJECTS.PRID (+)
        AND INV_INVESTMENTS.IS_ACTIVE = 1 
        AND INV_PROJECTS.IS_TEMPLATE != 1
        AND INV_INVESTMENTS.STAGE_CODE = PROJECT_STAGE.LOOKUP_CODE (+)
        AND PROJECT_STAGE.LOOKUP_TYPE (+) = 'INV_STAGE_TYPE'
        AND PROJECT_STAGE.LANGUAGE_CODE (+) = 'en'
        AND PROJECT_STAGE.PARENT_LOOKUP_CODE (+) = 'rta'
        AND IS_PROGRAM != 1

ORDER BY Nvl(INV_INVESTMENTS.CODE, '')

错误正在

的子查询的第一部分内抛出
NEW_OVERALL_STATUS AS OVERALL_STATUS

1 个答案:

答案 0 :(得分:0)

我在TARGETS_PLANNED错过了一个下划线(它读取了TARGETS PLANNED)。对不起,只是坏眼睛!