具有相同ID值的Sum Query不重复

时间:2015-01-19 18:05:00

标签: c# sql-server

SELECT s.[Statistics ID], s.[Player ID], s.[Game ID], s.[Season ID], s.[Goals Scored], s.[Assists Made], s.[Key Passes], s.[Successful Tackles], s.[Successful Interceptions], s.[Goals Conceded], p.[Player Name], p.[Player ID]
FROM Statistics$ s, Players$ p WHERE s.[Player ID]=p.[Player ID]

这就是显示on the preview

的内容

嘿伙计们,所以我对这个查询有一些问题,但我想要发生的是所有的值,比如Goals Scored,Assists Made,Key pass等,总结每个玩家ID制作它们只出现一次。

1 个答案:

答案 0 :(得分:3)

您需要使用GROUP BYSUM(...)

等汇总
SELECT DISTINCT s.[Statistics ID], s.[Player ID], s.[Game ID], s.[Season ID], 
    p.[Player Name], Stats.*
FROM Statistics$ s
    JOIN Players$ p ON s.[Player ID] = p.[Player ID]
    JOIN (
        SELECT s.[Player ID], SUM(s.[Goals Scored]) AS GoalsScored, 
            SUM(s.[Assists Made]) AS AssistsMade, SUM(s.[Key Passes]) AS KeyPasses, 
            SUM(s.[Successful Tackles]) AS SuccessfulTackles, 
            SUM(s.[Successful Interceptions]) AS SuccessfulInterceptions, 
            SUM(s.[Goals Conceded]) AS GoalsConceded
        FROM Statistics$ s
        GROUP BY s.[Player ID]
    ) AS Stats ON Stats.[Player ID] = s.[Player ID]

请注意,您必须对GROUP BY以外的任何列使用聚合函数,但您可以将结果与播放器详细信息一起加入,如上所述