Hadoop 寻找每个赛季得分最高的主队

Hadoop 寻找每个赛季得分最高的主队,hadoop,hive,hiveql,greatest-n-per-group,Hadoop,Hive,Hiveql,Greatest N Per Group,下面是我的蜂巢查询,试图找出每个赛季得分最高的主队 select t1.season , max(t1.TOTAL_Goals) as Highest_Score from (select season, home_team_id, sum(home_goals) TOTAL_Goals from game_kpark group by season, home_team_id ) as t1 group by t1.season 上述代码的结果如下表所示 t1.sea

下面是我的蜂巢查询,试图找出每个赛季得分最高的主队

select t1.season , max(t1.TOTAL_Goals) as Highest_Score
  from
 (select season, home_team_id, sum(home_goals) TOTAL_Goals
    from game_kpark
   group by season, home_team_id
 ) as t1
 group by t1.season
上述代码的结果如下表所示

t1.season   highest_score

20122013    118
20132014    179
20142015    174
20152016    173
20162017    204
20172018    185
如果我在
之后包括
t1.home\u team\u id
,请选择
,并在末尾按
分组,
它返回每个赛季球队的所有总分,而不是最高分

如何正确编写查询以查看每个赛季得分最高的相应团队?

使用
rank()
分析函数:

select s.season, s.home_team_id, s.TOTAL_Goals
from
(
select s.season, s.home_team_id, s.TOTAL_Goals, 
       rank() over(partition by season order by s.TOTAL_Goals desc) as rnk
  from
 (--team season totals
  select season, home_team_id, sum(home_goals) TOTAL_Goals 
    from game_kpark
   group by season, home_team_id
 ) s
) s
where rnk=1; --filter teams with highest rank per season
使用
rank()
分析函数:

select s.season, s.home_team_id, s.TOTAL_Goals
from
(
select s.season, s.home_team_id, s.TOTAL_Goals, 
       rank() over(partition by season order by s.TOTAL_Goals desc) as rnk
  from
 (--team season totals
  select season, home_team_id, sum(home_goals) TOTAL_Goals 
    from game_kpark
   group by season, home_team_id
 ) s
) s
where rnk=1; --filter teams with highest rank per season

很好用。非常感谢你@左撇子很好用。非常感谢你@左连接