在数据库Oracle上的子查询无效标识符

时间:2019-02-08 06:40:24

标签: oracle

我想从Oracle数据库的某些表中进行子查询,结果发现我的编码出现错误。错误信息说:

ORA-00904:“ T4”。“ PROD_RESULT”:无效的标识符 00904. 00000-“%s:无效的标识符” *原因:
*行动: 第7行错误:9列

这是我的代码;

SELECT
       T1.LINE_CD "MACHINE",
       T2.ITM_NM "ITEM NAME".
       T1.INST_NO "MANUFACTURING NO",
       T3.PROD_ST_SCHD_DT "START SCHEDULE",
       SUM(T4.PROD_RESULT)"TOTAL PRODUCTION",
       SUM(T4.PROD_RESULT)-T1.PROD_SCHD_QTY "OUTSTANDING"
  FROM ST_SASHIZU_ALL T1,
       CM_HINMO_ALL T2,
       TMCI_IM_PROD2 T3,
       (
        SELECT INST_NO, SUM(PROD_RESULT)"TOTAL PRODUCTION" 
          FROM TMCI_IM_PROD2 
         WHERE INST_NO='M190200101' 
      GROUP BY INST_NO) T4 
         WHERE 0=0
           AND T1.ITM_CD  = T2.ITM_CD
           AND T1.INST_NO = T3.INST_NO
           AND T1.INST_NO = T4.INST_NO
           AND T1.INST_NO='M190200101'
           AND T1.LINE_CD LIKE 'A%'
           AND T3.PROD_DT 
       BETWEEN TO_DATE(:qDT_FROM, 'DDMMYYYY') 
           AND TO_DATE(:qDT_TO, 'DDMMYYYY')
        HAVING SUM(T3.PROD_RESULT + 1) <= T1.PROD_SCHD_QTY 
      GROUP BY
               T1.LINE_CD,
               T2.ITM_NM,
               T1.INST_NO,
               T3.PROD_ST_SCHD_DT,
               T3.PROD_DT
      ORDER BY 1 ASC

我试图更改我的编码,但并没有帮助我。我也曾尝试在Google中找到其他方式,但也没有找到。请问有人在这里可以帮助我纠正我的问题。谢谢

1 个答案:

答案 0 :(得分:0)

这里的问题是您试图选择t4子查询中没有的列。

您应该做的是在子查询中使用一个更简单的别名,然后在外部选择中使用该别名,如下所示:

SELECT
       T1.LINE_CD "MACHINE",
       T2.ITM_NM "ITEM NAME".
       T1.INST_NO "MANUFACTURING NO",
       T3.PROD_ST_SCHD_DT "START SCHEDULE",
       SUM(T4.sum_prod_result) "TOTAL PRODUCTION",
       SUM(T4.sum_prod_result) - T1.PROD_SCHD_QTY "OUTSTANDING"
  FROM ST_SASHIZU_ALL T1,
       CM_HINMO_ALL T2,
       TMCI_IM_PROD2 T3,
       (
        SELECT INST_NO, SUM(PROD_RESULT) sum_prod_result
          FROM TMCI_IM_PROD2 
         WHERE INST_NO='M190200101' 
      GROUP BY INST_NO) T4 
         WHERE 0=0
           AND T1.ITM_CD  = T2.ITM_CD
           AND T1.INST_NO = T3.INST_NO
           AND T1.INST_NO = T4.INST_NO
           AND T1.INST_NO='M190200101'
           AND T1.LINE_CD LIKE 'A%'
           AND T3.PROD_DT 
       BETWEEN TO_DATE(:qDT_FROM, 'DDMMYYYY') 
           AND TO_DATE(:qDT_TO, 'DDMMYYYY')
        HAVING SUM(T3.PROD_RESULT + 1) <= T1.PROD_SCHD_QTY 
      GROUP BY
               T1.LINE_CD,
               T2.ITM_NM,
               T1.INST_NO,
               T3.PROD_ST_SCHD_DT,
               T3.PROD_DT
      ORDER BY 1 ASC;