【问题标题】:Hive sql: count and avgHive sql:计数和平均值
【发布时间】:2017-04-19 22:42:58
【问题描述】:

我最近正在尝试学习 Hive,但我遇到了一个 sql 咨询问题。 我有一个包含一些信息的 json 文件。我想获得每个寄存器的平均值。更好的例子:

country  times
USA      1
USA      1
USA      1
ES       1
ES       1
ENG      1
FR       1

然后与下一次咨询:

select country, count(*) from data;

我得到:

country   times
USA        3
ES         2
ENG        1 
FR         1

那我应该下一个了:

country   avg
USA       0,42  (3/7)
ES        0,28  (2/7)
ENG       0,14  (1/7)
FR        0,14  (1/7)

我不知道如何从第一个表中得到这个。

我试过了:

select t1.country, avg(t1.tm), 
from (
    select country,count(*)as tm from data where not country is null group by country
) t1
group by t1.country;

但我的输出是错误的。

感谢您的帮助!! BR。

【问题讨论】:

    标签: sql count hive average


    【解决方案1】:

    将每组计数除以总计数即可得到结果。使用Sub-Query 查找表中的记录总数

    试试这个

    select t1.country, count(*)/IFNULL((select cast(count(*) as float) from data),0)
    from data
    group by t1.country;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多