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

请注意,这有点狡猾,因为它在 SELECT 中有非聚合列,它们不在 GROUP BY 子句中。在这种情况下,使用默认 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 回答