如何在相关子查询中访问外部查询?

时间:2019-02-26 13:50:23

标签: sql apache-spark-sql

查询不与相关子查询一起运行。它将引发错误“不允许外部查询列”。如何更改查询以使其正常工作?

我想做的是,从MORD.mfgOrdrSttsCd中选择那些记录,这些记录没有子查询中存在的值。您可以看到一个有效的示例(用于说明目的)in this question and answer

我的查询如下

SELECT
    MORD.mfgOrdrSttsCd
FROM qa_edm_mfg_order AS MORD
WHERE (
    SELECT
        PPAR.parameterValue
    FROM qa_edm_mfg_order AS ABC
    JOIN qa_plan_cns_plan_parameter AS PPAR
    ON ABC.sourceSysCd = PPAR.sourceSystem
    WHERE PPAR.dataObject = 'PP'
    AND PPAR.`attribute` = 'SEND_TO_OMP'
    AND PPAR.`parameter` = 'ORDERSTAT'
    AND PPAR.inclExcl = 'E'
    AND MORD.mfgOrdrSttsCd LIKE CONCAT('%', PPAR.parameterValue ,'%')
    LIMIT 1
) IS NULL

我的错误如下。

SQL Error: org.apache.spark.sql.AnalysisException: Accessing outer query column is not allowed in:
LocalLimit 1
+- Project [parameterValue#10195]
   +- Filter ((((dataObject#10187 = PP) && (attribute#10194 = SEND_TO_OMP)) && (parameter#10192 = ORDERSTAT)) && ((inclExcl#10188 = E) && outer(mfgOrdrSttsCd#10127) LIKE concat(%, parameterValue#10195, %)))
      +- Join Inner, (sourceSysCd#10182 = sourceSystem#10191)
         :- SubqueryAlias ABC
         :  +- SubqueryAlias qa_edm_mfg_order
         :     +- Relation[_PK_#10140,actRlseDt#10141,chgDttm#10142,strtDt#10143,localStatus1#10144,plntCd#10145,objectNumber#10146,endDt#10147,actStrtDt#10148,schRelDt#10149,_MR_PK_#10150,mfgOrdrTypCd#10151,planRlseDt#10152,bomAltNum#10153,bomCatCd#10154,ordrRtngNum#10155,prdtnEndDt#10156,localSystemStatus#10157,rtngTypCd#10158,prdSpvsrCd#10159,rsrvtnNum#10160,actStrtTm#10161,schdEndDt#10162,cnfrmdScrpQty#10163,... 20 more fields] AdfGridRelation(/qa/edm/mfg_order,read.mode=0)
         +- SubqueryAlias PPAR
            +- SubqueryAlias qa_plan_cns_plan_parameter
               +- Relation[_PK_#10184,timeStamp#10185,valid#10186,dataObject#10187,inclExcl#10188,comments#10189,flatFile#10190,sourceSystem#10191,parameter#10192,modifiedBy#10193,attribute#10194,parameterValue#10195,status#10196,_UPT_#10197L] AdfGridRelation(/qa/plan/cns_plan_parameter,read.mode=0)
;
  org.apache.spark.sql.AnalysisException: Accessing outer query column is not allowed in:
LocalLimit 1
+- Project [parameterValue#10195]
   +- Filter ((((dataObject#10187 = PP) && (attribute#10194 = SEND_TO_OMP)) && (parameter#10192 = ORDERSTAT)) && ((inclExcl#10188 = E) && outer(mfgOrdrSttsCd#10127) LIKE concat(%, parameterValue#10195, %)))
      +- Join Inner, (sourceSysCd#10182 = sourceSystem#10191)
         :- SubqueryAlias ABC
         :  +- SubqueryAlias qa_edm_mfg_order
         :     +- Relation[_PK_#10140,actRlseDt#10141,chgDttm#10142,strtDt#10143,localStatus1#10144,plntCd#10145,objectNumber#10146,endDt#10147,actStrtDt#10148,schRelDt#10149,_MR_PK_#10150,mfgOrdrTypCd#10151,planRlseDt#10152,bomAltNum#10153,bomCatCd#10154,ordrRtngNum#10155,prdtnEndDt#10156,localSystemStatus#10157,rtngTypCd#10158,prdSpvsrCd#10159,rsrvtnNum#10160,actStrtTm#10161,schdEndDt#10162,cnfrmdScrpQty#10163,... 20 more fields] AdfGridRelation(/qa/edm/mfg_order,read.mode=0)
         +- SubqueryAlias PPAR
            +- SubqueryAlias qa_plan_cns_plan_parameter
               +- Relation[_PK_#10184,timeStamp#10185,valid#10186,dataObject#10187,inclExcl#10188,comments#10189,flatFile#10190,sourceSystem#10191,parameter#10192,modifiedBy#10193,attribute#10194,parameterValue#10195,status#10196,_UPT_#10197L] AdfGridRelation(/qa/plan/cns_plan_parameter,read.mode=0)
;
  org.apache.spark.sql.AnalysisException: Accessing outer query column is not allowed in:
LocalLimit 1
+- Project [parameterValue#10195]
   +- Filter ((((dataObject#10187 = PP) && (attribute#10194 = SEND_TO_OMP)) && (parameter#10192 = ORDERSTAT)) && ((inclExcl#10188 = E) && outer(mfgOrdrSttsCd#10127) LIKE concat(%, parameterValue#10195, %)))
      +- Join Inner, (sourceSysCd#10182 = sourceSystem#10191)
         :- SubqueryAlias ABC
         :  +- SubqueryAlias qa_edm_mfg_order
         :     +- Relation[_PK_#10140,actRlseDt#10141,chgDttm#10142,strtDt#10143,localStatus1#10144,plntCd#10145,objectNumber#10146,endDt#10147,actStrtDt#10148,schRelDt#10149,_MR_PK_#10150,mfgOrdrTypCd#10151,planRlseDt#10152,bomAltNum#10153,bomCatCd#10154,ordrRtngNum#10155,prdtnEndDt#10156,localSystemStatus#10157,rtngTypCd#10158,prdSpvsrCd#10159,rsrvtnNum#10160,actStrtTm#10161,schdEndDt#10162,cnfrmdScrpQty#10163,... 20 more fields] AdfGridRelation(/qa/edm/mfg_order,read.mode=0)
         +- SubqueryAlias PPAR
            +- SubqueryAlias qa_plan_cns_plan_parameter
               +- Relation[_PK_#10184,timeStamp#10185,valid#10186,dataObject#10187,inclExcl#10188,comments#10189,flatFile#10190,sourceSystem#10191,parameter#10192,modifiedBy#10193,attribute#10194,parameterValue#10195,status#10196,_UPT_#10197L] AdfGridRelation(/qa/plan/cns_plan_parameter,read.mode=0)
;

0 个答案:

没有答案