2017-11-07 12 views
0

クエリ(編集済みフォーマット):マックス()関数()

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; 

私はマックス(のために間違った答えを取得しています)私が間違っているつもりですどこ私に知らせてください。?

+0

は次のリンクを参照してください。 https://stackoverflow.com/questions/7759347/can-we-use-sum-and-max-function-with-each-other-in-sql-statement – varman

答えて

0

SQLテーブルも共有してもよろしいですか?

MAX()のソースがSUM()のものであることが間違っているとお考えでした。

目標としてMAX(TotalGoals)を適用しましたが、SUM(TeamGoals)AS TotalGoals、 を適用したのは明らかです.TotalGoalsはSUM(TeamGoals)の結果であり、MAX()funcは===> MAX (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; 
関連する問題