如何在HIVEQL中的特定列值上进行外部联接?

时间:2018-03-28 17:21:52

标签: hadoop hive hiveql

当我运行以下HiveQL代码时,我收到错误:     执行错误:编译语句时出错:FAILED:SemanticException [错误10004]:第1行:2112无效的表别名或        列引用'T3'

    SELECT *        
    FROM CC_CLAIM_EXT T1
    INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
    LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID)
    LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
    LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
    LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
    LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
    LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
    LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)

    LEFT OUTER JOIN (SELECT TT12.CLAIMID
                            ,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
                            ,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
                    FROM CC_INCIDENT_EXT TT12
                    LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
                    GROUP BY TT12.CLAIMID) T14 
    ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))

    WHERE T1.STATE IN(2,3)
        AND T2.STATE IN(2,3)
        AND T6.APPROVALSTATUS = 1
        AND T7.RETIRED = 0

    ORDER BY CLAIMNUMBER
        ,EXPOSUREID
        ,TRANSACTIONID

我把它缩小到了这一行:

    ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))

如果我删除:

    AND T3.POLICYTYPE IN(10022,10023)

代码运行正常。有没有更好的方法来限制HiveQL中的这种连接?

1 个答案:

答案 0 :(得分:0)

错误是因为你在T1和T14之间的左连接上放了一个引用但是你把T3放在条件上。要将查询限制为指定的T3.policy_id,您应该沿着离开连接T1和T3的行放入。见第4行:

SELECT *        
    FROM CC_CLAIM_EXT T1
    INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
    LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID AND T3.POLICYTYPE IN(10022,10023))
    LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
    LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
    LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
    LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
    LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
    LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)
    LEFT OUTER JOIN (SELECT TT12.CLAIMID
                            ,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
                            ,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
                    FROM CC_INCIDENT_EXT TT12
                    LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
                    GROUP BY TT12.CLAIMID) T14 
    ON T1.ID = T14.CLAIMID
    WHERE T1.STATE IN(2,3)
        AND T2.STATE IN(2,3)
        AND T6.APPROVALSTATUS = 1
        AND T7.RETIRED = 0
    ORDER BY CLAIMNUMBER
        ,EXPOSUREID
        ,TRANSACTIONID