您有下表: 团队,teamPoints,team_members,pointsTable 我正在尝试SUM团队成员积分,然后总结团队积分:
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
但似乎改变/乘以原始值 还尝试过subselects但需要一些如何组?
欢迎任何帮助
答案 0 :(得分: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