查询(编辑格式):
SELECT
league_id as League, TeamId, MAX(TotalGoals) as Goals,season
FROM
(SELECT
league_id, TeamId, SUM(TeamGoals) AS TotalGoals, season
FROM
(SELECT
league_id,
season ,
home_team_api_id AS TeamId,
SUM(home_team_goal) AS TeamGoals
FROM
games
GROUP BY TeamId , league_id , season UNION SELECT
league_id,
season,
away_team_api_id AS TeamId,
SUM(away_team_goal) AS TeamGoals
FROM
games
GROUP BY TeamId , league_id , season) t1
GROUP BY TeamId , season) g3
GROUP BY league_id , season;
我对Max()的回答错误。请告诉我哪里出错了?
答案 0 :(得分:0)
您是否也想分享您的SQL表?
我猜你用MAX()得到了错误的答案,因为MAX()的来源是来自SUM()。
您将MAX(TotalGoals)应用为目标,但同时也是SUM(TeamGoals)AS TotalGoals, 显然,TotalGoals是SUM(TeamGoals)的结果,MAX()函数将变为===> MAX(SUM(TeamGoals)),它肯定会返回SUM()的相同结果。
好吧,期待能够分享他们观点的其他开发者......
我复制了SQL并将其格式化为下面的SQL美化器:
SELECT
league_id as League,
TeamId,
MAX(TotalGoals) as Goals,
season
FROM
(
SELECT
league_id,
TeamId,
SUM(TeamGoals) AS TotalGoals,
season
FROM
(
SELECT
league_id,
season,
home_team_api_id AS TeamId,
SUM(home_team_goal) AS TeamGoals
FROM
games
GROUP BY
TeamId,
league_id,
season
UNION
SELECT
league_id,
season,
away_team_api_id AS TeamId,
SUM(away_team_goal) AS TeamGoals
FROM
games
GROUP BY
TeamId,
league_id,
season
)
t1
GROUP BY
TeamId,
season
)
g3
GROUP BY
league_id,
season;