Hive 查询以获取最大计数

Posted

技术标签:

【中文标题】Hive 查询以获取最大计数【英文标题】:Hive query to get max of count 【发布时间】:2014-07-15 11:14:07 【问题描述】:

我的输入文件是这样的:

id,phnName,price,model
1,iphone,2000,abc
2,iphone,3000,abc1
3,nokia,4000,abc2
4,sony,5000,abc3
5,nokia,6000,abc4
6,iphone,7000,abc5
7,nokia,8500,abc6

我想编写一个配置单元查询来获取特定手机的最大计数。

输出:

iphone 3
nokia 3

到目前为止,我已经尝试了以下查询:

select d.phnName,count(*) from phnDetails d group by d.phnName

得到这样的输出:

iphone 3
nokia 3
sony 1

帮助我只检索最大值。

【问题讨论】:

提供了如下答案,请查看并告诉我 【参考方案1】:

我的查询正在运行

输入

1,iphone,2000,abc
2,iphone,3000,abc1
3,nokia,4000,abc2
4,sony,5000,abc3
5,nokia,6000,abc4
6,iphone,7000,abc5
7,nokia,8500,abc6

工作查询

select c.phnName, c.counter 
from 
(select d.phnName as phnName, count(*) as counter from phnDetails d group by d.phnName ) c 
join 
(select max(f.counter) as countmax from
(select cnt.phnName as phnName, count(*) as counter from phnDetails cnt group by cnt.phnName ) f) g 
where c.counter = g.countmax;

不确定它是否经过优化,但它按预期工作

输出如下,

使用以下步骤解决,如果回答了您的问题,请将其标记为已回答

CREATE TABLE phnDetails ( id INT, phnName STRING, price INT, details STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

LOAD DATA LOCAL INPATH '/home/training/Phone/phones.txt' OVERWRITE INTO TABLE phnDetails;

select * from phnDetails;


select d.phnName, count(*) from phnDetails d group by d.phnName;


select c.phnName, c.counter from 
(select d.phnName as phnName, count(*) as counter from phnDetails d group by d.phnName ) c ;

select max(f.counter) as countmax from
(select cnt.phnName as phnName, count(*) as counter from phnDetails cnt group by cnt.phnName ) f ;

select max(f.counter) as countmax from
(select cnt.phnName as phnName, count(*) as counter from phnDetails cnt group by cnt.phnName ) f ;

select c.phnName, c.counter 
from 
(select d.phnName as phnName, count(*) as counter from phnDetails d group by d.phnName ) c 
join 
(select max(f.counter) as countmax from
(select cnt.phnName as phnName, count(*) as counter from phnDetails cnt group by cnt.phnName ) f) g 
where c.counter = g.countmax;

【讨论】:

感谢您将我的解决方案标记为已回答,您能否也为我的解决方案投票。感谢您的快速确认

以上是关于Hive 查询以获取最大计数的主要内容,如果未能解决你的问题,请参考以下文章

Hive 查询以查找中间几周的计数

通过DataFrame查询计数查询时如何获取整数值?

获取最大计数的行

如何在 hbase 表中获取计数记录?查询记录的最快方法是啥?

用于获取不同日期以及唯一计数最大值的大查询

如何使用 group by(基于一列)从表中选择多列,在 hive 查询中具有和计数