【问题标题】:hiveQL if statements regroup on one rowhiveQL if 语句在一行上重新组合
【发布时间】:2013-01-26 01:33:15
【问题描述】:

查询:

select IF(type='view', count(*), 0), IF(type='click', count(*), 0)
from ad_events
where year=2013 and month=01 and day=18 and (hour=01 or hour=02)
group by type

结果:

      _c0       _c1
0      0         0
1      0         0
2      0       1368
3      0         0
4      0         0
5      0         0
6      0         0
7      0         0
8   277917       0
9      0         0

有没有这样的结果只在一行上? :

      _c0       _c1
0    277917     1368

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    好的,我只是在嵌套查询中做到了:

    select SUM(c), SUM(v)
    from (
    select tracking_id, IF(type='view', count(*), 0) AS v, IF(type='click', count(*), 0) AS c
    from ad_events
    where year=2013 and month=01 and day=18
    group by tracking_id, type
    ) t2
    

    【讨论】:

    • 谢谢 - 我以前从未想过在“IF”中进行计数。你得到这么多“空”行的原因是“类型”有(可能)10个不同的可能值,你按“类型”分组,但没有选择它。这不会停止分组,而且看起来你的结果太多了。但是子查询会处理这个问题。谢谢
    • hm,如果聚合对我不起作用,我会得到“表达式不在 GROUP BY 键 '0' 中”
    【解决方案2】:

    请尝试关注

    select
       type,
       count(type) TypeCount
    from ad_events
    where
       year=2013 and 
       month=01 and 
       day=18 and 
       (hour=01 or 
        hour=02)
    group by type
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-17
      • 2021-10-25
      • 1970-01-01
      • 2023-03-12
      • 2010-12-31
      • 2018-07-22
      • 1970-01-01
      • 2020-05-08
      相关资源
      最近更新 更多