こんにちは、以下のテーブルがあります: teams, teamPoints, team_members, pointsTable チームメンバーのポイントを合計してから、チームのポイントを合計しようとしています:
Teams
|TeamID |TeamName | TeamLocation|
-------------------------------
|1 | sm1 | location1|
|2 | sm2 | location2|
TeamPoints
|TeamID |TotalPonts | RemainderPonts|
-------------------------------
|1 | 10 | 7 |
|1 | 8 | 6 |
|2 | 8 | 6 |
team_members
|TeamID |UserID |
-----------------
|1 | 1 |
|1 | 2 |
|2 | 3 |
pointsTable
|UserID |TotalPonts | RemainderPonts|
-------------------------------
|1 | 10 | 7 |
|2 | 8 | 6 |
|2 | 8 | 6 |
so results for team sm1 (TeamID =1) should be
TeamName=sm1,TeamPoints.TotalPonts =18, TeamPoints.RemainderPonts=13
pointsTable.TotalPonts =26, pointsTable.RemainderPonts=19
これはメンバーのポイントを合計するのに機能しますが、チームのポイントに問題があります
SELECT
teams.TeamID AS theTeamID,
teams.TeamName,
teams.TeamLocation,
team_members.TeamID,
team_members.UserID,
SUM(pointstable.RemainderPoints) AS points
FROM
teams
LEFT JOIN
team_members ON teams.TeamID = team_members.TeamID
LEFT JOIN
pointstable ON team_members.UserID = pointstable.UserID
GROUP BY teams.TeamID
試しました
SELECT
teams.TeamID AS theTeamID,
teams.TeamName,
teams.TeamLocation,
team_members.TeamID,
team_members.UserID,
SUM(pointstable.RemainderPoints) AS points,
teampoints.TeamID AS tpID,
SUM(teampoints.TotalPoints) AS teamRedeamable
FROM
teams
LEFT JOIN
team_members ON teams.TeamID = team_members.TeamID
LEFT JOIN
pointstable ON team_members.UserID = pointstable.UserID
LEFT JOIN
teampoints ON teams.TeamID = teampoints.TeamID
GROUP BY teams.TeamID
しかし、元の値を変更/乗算するようですサブセレクトも試しましたが、どのようにグループ化する必要がありますか??
どんな助けでも大歓迎です