我有以下 MS SQL 查询,它交叉引用三个表(tTeam、tPlayer 和 tScores)来获得按 Net score 和 Team 排序的总“Net score”、“Gross score”和“Position”。
SELECT TeamID, Team, NetScore, Gross,
CASE WHEN cnt > 1 THEN 'T' + CAST(rnk AS VARCHAR(5))
ELSE CAST(rnk AS VARCHAR(5))
END Pos
FROM (
SELECT tTeam.TeamID,
tTeam.Title AS Team,
SUM(CONVERT(INT, tScores.Net_Score)) AS NetScore,
SUM(CONVERT(INT, tScores.Out_Score) + CONVERT(int, tScores.In_Score)) AS Gross,
rank() OVER ( ORDER BY SUM(CONVERT(INT, tScores.Net_Score))) rnk,
COUNT(*) OVER ( PARTITION BY SUM(CONVERT(INT, tScores.Net_Score))) cnt
FROM tScores INNER JOIN tPlayer ON tScores.PlayerID = tPlayer.PlayerID INNER JOIN tTeam ON tPlayer.TeamID = tTeam.TeamID
WHERE tTeam.TournamentID = 13
GROUP BY tTeam.TeamID, tTeam.Title ) temp
ORDER BY NetScore, Team
该查询效果很好,但是(这是我需要帮助的地方),它正在按团队计算所有球员的净得分和总得分,而我需要做的只是按团队计算“4 个最低球员的净得分和总得分”只要。
我花了最后一天半的时间用这个把头发拉出来,任何帮助都将不胜感激。
提前致谢!