【问题标题】:How do you group OR clause in WHERE statement using HIVE如何使用 HIVE 在 WHERE 语句中对 OR 子句进行分组
【发布时间】:2015-07-08 23:51:02
【问题描述】:

我想通过以下方式查询

(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


    【解决方案1】:
    1. count 是真正的变量名吗?仔细检查。
    2. 还要检查以确保您的时间是数字类型,可能是 bigint。如果它不是这样的大整数:

      WHERE cast(time as bigint) >= 1435536000 AND cast(time as bigint) < 1436140800
      
    3. 尝试将 or 语句更改为 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;
      

    一次尝试每个更改,这样您就知道修复了什么。

    【讨论】:

    • in 语法有效,我很惊讶你不能用括号对逻辑语句进行分组
    【解决方案2】:

    总是学会在关系数据库中使用 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
    

    【讨论】:

      【解决方案3】:

      查询1:

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

      查询2:

      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
      

      【讨论】:

      • 这不是问题的答案,请正确阅读问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 2010-11-13
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多