我的输入文件是这样的:
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,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;