[英]SQL Server : SUM column with same ID
I am new to SQL and experimenting with a database of baseball stats.我是 SQL 的新手,正在尝试使用棒球统计数据库。 I would like to write a query that displays the total hits by each player over their career.我想写一个查询来显示每个球员在他们的职业生涯中的总命中数。 I know how to SUM the hits of an individual player using:我知道如何使用以下方法对单个玩家的点击数求和:
Select Sum(H)
From Batting
Where PlayerID = 'aaronha01'
Result = 3771结果 = 3771
but I can not figure out how to query every player at once.但我不知道如何一次查询每个玩家。 I would like the result of my query to look something like this:我希望我的查询结果看起来像这样:
|PlayerID|, |Hits|
|player 1|, |3771|
|player 2|, |5562|
etc...
Any help would be greatly appreciated.任何帮助将不胜感激。 I have included a screenshot of the table if it that provides help to you.如果它为您提供帮助,我已经包含了表格的屏幕截图。
Select playerID, sum(H) Hits From Batting Group By playerID Select playerID, sum(H) Hits From Batting Group By playerID
SUM is used with a GROUP BY clause. SUM 与 GROUP BY 子句一起使用。
The aggregate functions summarize the table data.聚合函数汇总表数据。 Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group.将行分成组后,将应用聚合函数以便每组仅返回一个值。 It is better to identify each summary row by including the GROUP BY clause in the query resulst.最好通过在查询结果中包含 GROUP BY 子句来识别每个汇总行。 All columns other than those listed in the GROUP BY clause must have an aggregate function applied to them.除 GROUP BY 子句中列出的列之外的所有列都必须应用聚合 function。
SELECT PlayerID, SUM(H) AS 'Hits' FROM Batting GROUP BY PlayerID SELECT PlayerID, SUM(H) AS 'Hits' from Batting GROUP BY PlayerID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.