如何使用方法max PostgreSQL

时间:2014-03-13 01:50:16

标签: sql postgresql

我需要从两个表中查找信息 - 每个团队每个赛季的总进球数。这就是我如何做到的:

SELECT Team, sum(G) as "Total Goals"
From Stats, HockeyPlayer
Where HockeyPlayer.Player_ID = Stats.Player_ID
group by Team;

现在,我需要找到哪支球队得分最多。如何在这里使用方法max? 谢谢

1 个答案:

答案 0 :(得分:0)

此查询可以为您提供帮助。

SELECT 
  *, MAX(goals) AS m_goals 
FROM (
  SELECT Team, sum(G) as goals
  From Stats, HockeyPlayer
  Where HockeyPlayer.Player_ID = Stats.Player_ID
  group by Team
  ) as t
WHERE 
    goals=m_goals

尝试使用此查询,而不是之前的查询。