从hql中max(列)上的表中选择值

qlzsbp2j  于 2021-06-29  发布在  Hive
关注(0)|答案(3)|浏览(420)

我对hql还相当陌生,仍然在尝试如何理解语法。我有一个表'得分'与playerid和致胜球(gwg)。我想查看拥有最大gwg的玩家。我的问题是:

select playerid as ID from scoring where gwg = ('select max(scoring.gwg) from scoring');

虽然hive打印列的名称,但我没有得到输出。但是当我硬编码“gwg=16”时,我得到了我需要的输出。有什么建议吗?我正在做hortonworks2.3.2。

vxqlmq5t

vxqlmq5t1#

我想你只需要这个简单的问题

select playerid,max(scoring.gwg) as max from scoring group by playerid order by max desc

配置单元不支持子查询。

sgtfey8w

sgtfey8w2#

有两种方法可以做到这一点。获取最大计数并用最大gwg过滤playerid

select playerid
from scoring 
group by playerid 
having count(*) = ( select max(c) from 
                (
                   select playerid,count(*) c 
                   from scoring 
                   group by playerid
                ) a
              )

或者得到计数,按降序排列,得到最上面的一行

select playerid, count(*) as cnt
from scoring 
group by playerid 
order by cnt desc
limit 1;
mccptt67

mccptt673#

你可以试试这个

select playerid, max(scoring.gwg) from scoring group by playerid;

这是另一个

select playerid, max(scoring.gwg) as maxScore from scoring 
   group by playerid order by maxScore desc;

上面将给出玩家ID的降序排列,即得分最高的将在上面。

相关问题