WHERE中有多个IN子查询

时间:2017-04-14 09:14:30

标签: sql hadoop hive cloudera

我在尝试将以下查询从impala转换为cloudera 5.8上的hive 1.1时遇到问题。

SELECT   *
FROM   
table1 t1,table2 t2
WHERE   concat(t1.field1, t1.field2) IN
               (SELECT   concat(T3.field1, T3.field2)
                  FROM   table3 T3
                 WHERE   T3.field3 = 'value')
         AND concat(t1.field3, t1.field4) IN
               (SELECT   concat(T3.field1, T3.field2)
                  FROM   table3 T3
                 WHERE   T3.field3 = 'value')
AND t1.some_field = t2.some_field

我在这里得到的错误表明我不能在where子句中做多个子查询。

  

仅支持1个SubQuery表达式。

我尝试使用union解决此问题,但在此版本中仅支持union all。我不太确定如何在这里使用连接来解决这个问题。

我很感激有关如何重写此查询的建议,以便在不抛出错误的情况下生成预期结果。

3 个答案:

答案 0 :(得分:1)

使用联接和CTE:

with s3 as (SELECT T3.field1, T3.field2
                         FROM   table3 T3
                        WHERE   T3.field3 = 'value')

SELECT   *
FROM   
table1 t1 
       inner join table2 t2 on t1.some_field = t2.some_field
       left semi join s3 on t1.field1=s3.field1 
                        and t1.field2=s3.field2
       left semi join s3 on t1.field3=s3.field1 
                        and t1.field4=s3.field2

答案 1 :(得分:0)

他们的文件说你可以使用CTE。 https://cwiki.apache.org/confluence/display/Hive/Common+Table+Expression

你能试试吗?

WITH firstConcatResult AS (
    SELECT * FROM 
    table1 t1,table2 t2
    WHERE 
        //first concat
)
SELECT * FROM firstConcatResult f
WHERE  
    //other concat

答案 2 :(得分:-1)

我会使用exists和正确的join语法:

SELECT *
FROM table1 t1 JOIN
     table2 t2
     ON t1.some_field = t2.some_field
WHERE EXISTS (SELECT 1
              FROM table3 T3
              WHERE T3.field3 = 'value' AND
                    T3.field1 = t1.field1 AND t3.field2 = t1.field2
             ) AND
      EXISTS (SELECT 1
              FROM table3 T3
              WHERE T3.field3 = 'value' AND
                    T3.field1 = t1.field3 AND t3.field2 = t1.field4
             );