如何使用HIVE在WHERE语句中对OR子句进行分组

时间:2015-07-08 23:51:03

标签: sql hadoop hive bigdata

我想通过以下

查询
(statement1 AND statement2 AND (statement3 OR statement4 ))

这是我的hive查询,我确认它没有工作,因为它只返回statement3,我知道有些情况,其中statement4为真

SELECT 
  cid,
  SUM(count) AS total_count
FROM
  count_by_day
WHERE
  time >= 1435536000
  AND time < 1436140800
  AND(
    cid = '4eb3441f282d4d657a000016'
    OR cid = '14ebe153121a863462300043d'
  )
GROUP BY
  cid

有人能告诉我什么是错的吗?感谢

3 个答案:

答案 0 :(得分:4)

  1. 算一个真正的变量名吗?仔细检查一下。
  2. 同时检查以确保您的时间是数字类型,可能是一个bigint。如果它不是像这样把它作为一个大的int:

    WHERE cast(time as bigint) >= 1435536000 AND cast(time as bigint) < 1436140800
    
  3. 尝试将您的或声明更改为in语句。

    SELECT 
     cid,
     SUM(count) AS total_count
    FROM
    count_by_day
    WHERE time >= 1435536000 AND time < 1436140800
    AND cid in('4eb3441f282d4d657a000016','14ebe153121a863462300043d')
    GROUP BY
    cid;
    
  4. 一次尝试一次更改,以便了解修复内容。

答案 1 :(得分:0)

查询1:

Select A,B,C
From 
                Table1 t1
                Join 
                Table2 t2
                On 
                                T1.field1=T2.field1 OR 
                                T1.field2 = T2.field2

QUERY2:

Select A,B,C
From 
                Table1 t1
                Join 
                Table2 t2
                On 
                                T1.field1=T2.field1 

Union 

Select A,B,C
From 
                Table1 t1
                Join 
                Table2 t2
                On 
                                T1.field2 = T2.field2

答案 2 :(得分:0)

始终学会在关系数据库中使用UNION代替OR。尝试看看union是否能解决您的问题。

select cols
from table
where statement1 AND statement2 AND statement3

union all

select cols
from table
where statement1 AND statement2 AND statement4