【问题标题】:Fetching distinct combination of column values and their count in Hive在 Hive 中获取列值及其计数的不同组合
【发布时间】:2018-01-12 12:34:35
【问题描述】:

我有一个包含两列名称和值的表。我想计算每个组合在 Hive 中重复的次数

          Alex  Biology
          Joe  Chemistry
          Alex  Physics
          Alex  Physics
          Joe   Physics
          Joe   Physics

我希望输出像

          Alex  Biology  1
          Joe  Chemistry 1
          Alex  Physics  2
          Joe   Physics  2

我尝试了类似 SQL 的计数查询 从 tbl 中选择不同的名称、值、计数(*) 但这失败并出现错误“UDAF 计数尚不支持位置” 我尝试使用 collect_set 但也没有成功

        select x.name, x.value
           , num_arr
       from (
       select *
          , count(collect_set(name) over (partition by value)) num_arr
           from count_unique ) x

【问题讨论】:

    标签: hive


    【解决方案1】:

    您可以使用简单的group_by 函数并计算组合。试试这个

    select x.name, x.value, count(*) from count_unique group by x.name,x.value;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      相关资源
      最近更新 更多