用于impala的case语句内的select语句

时间:2019-06-24 10:17:50

标签: hive impala

在这种情况下,我需要消除硬编码的值,并且已经开发了一个用于获取值的查找表。

查找表如下:

serial_no   code    description type

1       J   Jol           CENTRE_TYPE
3       C   Cols          CENTRE_TYPE
4       R   Rud           CENTRE_TYPE
2       A   TCD           CENTRE_TYPE

我有以下黑斑羚代码:

SELECT `prtc`,
    CASE   WHEN LEFT(prtc,1) = 'C' THEN  (SELECT DESCRIPTION FROM dev.lookup_table WHERE CODE='C' AND TYPE='CENTRE_TYPE')
            WHEN LEFT(prtc,1) = 'A' THEN (SELECT DESCRIPTION FROM dev.lookup_table WHERE CODE='A' AND TYPE='CENTRE_TYPE')
            WHEN LEFT(prtc,1) = 'R' THEN (SELECT DESCRIPTION FROM dev.lookup_table WHERE CODE='R' AND TYPE='CENTRE_TYPE')
            WHEN LEFT(prtc,1) = 'J' THEN (SELECT DESCRIPTION FROM dev.lookup_table WHERE CODE='J' AND TYPE='CENTRE_TYPE')
    END AS CENTRE_TYPE
FROM dev.`tablename`

我收到一个错误,提示为“ AnalysisException:无法解析表引用:'dev.lookup_table'”

1 个答案:

答案 0 :(得分:0)

使用连接:

SELECT t.prtc,
       l.DESCRIPTION  as CENTRE_TYPE
FROM dev.`tablename` t
     left outer join dev.lookup_table l on LEFT(t.prtc,1)= l.CODE and l.TYPE='CENTRE_TYPE'