2016-12-26 115 views
0

我使用下面的查詢在蜂巢:where子句中使用「IN」爲子查詢在HIVE

SELECT COUNT(DISTINCT INDIV_ID) FROM TABLEA 
    WHERE CAR1="BLUE" AND INDIV_ID 
    IN (SELECT DISTINCT INDIV_ID FROM TABLEA WHERE CAR2="RED" AND CAR3="WHITE") 

我現在已經明白子查詢與條款不蜂巢工作。上面的查詢只是爲了理解Hive的子查詢的替代。

有人可以告訴我如何在Hive中寫上述查詢嗎?

+0

您使用的是哪個版本的Hive? –

回答

0

您可以使用CTE, 我已經添加了「CAR_R_W」CTE。下面的 是修改的查詢。

WITH CAR_R_W 
AS (
    SELECT DISTINCT INDIV_ID 
    FROM TABLEA 
    WHERE CAR2 = "RED" 
     AND CAR3 = "WHITE" 
    ) 
SELECT COUNT(DISTINCT INDIV_ID) 
FROM TABLEA 
WHERE CAR1 = "BLUE" 
    AND INDIV_ID IN (
     SELECT INDIV_ID 
     FROM CAR_R_W 
     );