我最近正在尝试学习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.
发布于 2016-12-05 21:59:18
将每组计数除以总计数即可得到结果。使用Sub-Query查找表中的记录总数
尝尝这个
select t1.country, count(*)/IFNULL((select cast(count(*) as float) from data),0)
from data
group by t1.country;https://stackoverflow.com/questions/40975955
复制相似问题