从HQL中的max(列)表中选择值

时间:2016-05-06 01:57:39

标签: hive hql reserved-words

我是HQL的新手,仍然试图通过语法来解决问题。我有一个表'得分'与playerID和游戏获胜目标(GWG)。我想查看拥有最大GWG的玩家ID。这是我的问题:

   <div class="panel panel-default">
       <div class="panel-heading">Urgence</div>
       <div class="panel-body">
          <div class="form-group">
             <label for="memberEmergencyContact" class="col-sm-3 control-label">Contact en cas d'urgence</label>
             <div class="col-sm-9">
                <input type="text" class="form-control" id="memberEmergencyContact" name="emergencyContactName" placeholder="Entrer le contact en cas d'urgence">
             </div>
          </div>
          <div class="form-group">
             <label for="emergencyPhone" class="col-sm-3 control-label">Tél</label>
             <div class="col-sm-9">
                <input type="text" class="form-control" id="emergencyPhone" name="emergencyPhone">
             </div>
          </div>
       </div>
    </div>

虽然Hive打印了列的名称,但我没有得到输出。但是当我硬编码'gwg = 16'时,我得到了我需要的输出。有什么建议?我正在研究Hortonworks 2.3.2。

3 个答案:

答案 0 :(得分:2)

你可以试试这个

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,即最高分的玩家ID将在最顶层。

答案 1 :(得分:2)

有几种方法可以做到这一点。 获取最大计数并使用max gwg

过滤播放器
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;

答案 2 :(得分:0)

我认为您只需要这个简单的查询

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

Hive不支持子查询。