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

问题描述 投票:1回答:3

我想通过以下查询

(statement1 AND statement2 AND (statement3 OR statement4 ))

这是我的hive查询,我验证它不起作用,因为它只返回statement3,我知道有些情况,其中statement4为true

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

谁能告诉我有什么问题?谢谢

sql hadoop hive bigdata
3个回答
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;

一次尝试一次更改,以便了解修复的内容。


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

0
投票

始终学会在关系数据库中使用UNION而不是OR。试试看联盟是否解决了你的问题。

select cols
from table
where statement1 AND statement2 AND statement3

union all

select cols
from table
where statement1 AND statement2 AND statement4
© www.soinside.com 2019 - 2024. All rights reserved.