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 查询以获取最大计数的主要内容,如果未能解决你的问题,请参考以下文章