1

こんにちは、以下のテーブルがあります: 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

しかし、元の値を変更/乗算するようですサブセレクトも試しましたが、どのようにグループ化する必要がありますか??

どんな助けでも大歓迎です

4

1 に答える 1

1

ユーザー ポイント テーブルから詳細を取得するにはサブセレクトが必要だと考え、これをメイン テーブルに結合します。

SELECT 
    teams.TeamID AS theTeamID,
    teams.TeamName,
    teams.TeamLocation,
    SUM(pointstable.RemainderPoints) AS RemainderPonts,
    SUM(pointstable.TotalPonts ) AS TotalPonts,
    Sub1.UserTotalPoints, 
    Sub1.UserRemainderPoints,
    Sub2.TeamTotalPoints,
    Sub2.TeamRemainderPoints
FROM teams
LEFT JOIN team_members ON teams.TeamID = team_members.TeamID
LEFT JOIN pointstable ON team_members.UserID = pointstable.UserID
LEFT OUTER JOIN
(
    SELECT TeamID, SUM(TotalPoints) AS UserTotalPoints, SUM(RemainderPoints) AS UserRemainderPoints
    FROM team_members
    INNER JOIN pointsTable
    ON team_members.UserID = pointsTable.UserID
    GROUP BY TeamID
) Sub1
ON Sub1.TeamID = teams.TeamID
LEFT OUTER JOIN
(
    SELECT TeamID, SUM(TotalPoints) AS TeamTotalPoints, SUM(RemainderPoints) AS TeamRemainderPoints
    FROM TeamPoints
    GROUP BY TeamID
) Sub2
ON Sub0.theTeamID = Sub2.TeamID
GROUP BY teams.TeamID, teams.TeamName, teams.TeamLocation

GROUP BY 句にない SELECT に非集計列があるため、これは少し危険であることに注意してください。この場合、デフォルトの MySQL 設定ではこれで問題ありませんが、別のデータベースを使用する場合 (または MySQL の設定によっては)、2 つの異なるサブセレクトとして実行し、結果を結合する必要がある場合があります。

SELECT Sub0.theTeamID, Sub0.TeamName, Sub0.TeamLocation, Sub0.RemainderPonts, Sub0.TotalPonts, Sub1.UserTotalPoints, Sub1.UserRemainderPoints, Sub2.TeamTotalPoints, Sub2.TeamRemainderPoints
FROM 
(
    SELECT 
        teams.TeamID AS theTeamID,
        teams.TeamName,
        teams.TeamLocation,
        SUM(pointstable.RemainderPoints) AS RemainderPonts,
        SUM(pointstable.TotalPonts ) AS TotalPonts
    FROM teams
    LEFT OUTER JOIN team_members ON teams.TeamID = team_members.TeamID
    LEFT OUTER JOIN pointstable ON team_members.UserID = pointstable.UserID
) Sub0
LEFT OUTER JOIN
(
    SELECT TeamID, SUM(TotalPoints) AS UserTotalPoints, SUM(RemainderPoints) AS UserRemainderPoints
    FROM team_members
    INNER JOIN pointsTable
    ON team_members.UserID = pointsTable.UserID
    GROUP BY TeamID
) Sub1
ON Sub0.theTeamID = Sub1.TeamID
LEFT OUTER JOIN
(
    SELECT TeamID, SUM(TotalPoints) AS TeamTotalPoints, SUM(RemainderPoints) AS TeamRemainderPoints
    FROM TeamPoints
    GROUP BY TeamID
) Sub2
ON Sub0.theTeamID = Sub2.TeamID
于 2013-10-15T11:30:04.470 に答える