0

我的数据库(SQL Server 2008 R2)中有一个这样的表:

 ID......Team...........Name......Age
 102     Barcelona      Mike      15
 103     Barcelona      Peter     10
 104     Barcelona      Jacke     10
 105     Barcelona      Jonas     10
 106     Real Madrid    Michael   20
 107     Real Madrid    Terry     26
 108     Chelsea        James     26
 109     Chelsea        Arthur    23
 110     Chelsea        Spence    22
  1. 我如何循环“团队”字段并知道,有像巴塞罗那、皇家马德里和切尔西这样的记录。
  2. 之后我想计算每个团队的团队球员的总和。

巴塞罗那:-> 10 + 10 + 10 + 15 = 45

对于皇家马德里:-> 20 + 26 = 46

切尔西:-> 26 + 23 + 22 = 71

  1. 将每个结果填充到单独的变量中。

整个计算应该在存储过程中完成。

第二件事,如果我有这样的表:

 ID......Team...........Name......HeaderGoal......FreeKickGoal
 104     Barcelona      Mike      2               1
 105     Barcelona      Peter     1               0
 106     Real Madrid    Michael   0               1
 107     Real Madrid    Terry     0               1
 108     Chelsea        James     0               0
 109     Chelsea        Arthur    2               3
 110     Chelsea        Spence    4               0
  1. 我如何循环“团队”字段并知道,有像巴塞罗那、皇家马德里和切尔西这样的记录。
  2. 之后,我想用进球类型 HeaderGoal 和 FreeKickGoal 计算每支球队的所有进球的总和。

示例

-> 巴塞罗那:2+1+1 = 4

-> 皇家马德里:1+1 = 2

-> 切尔西:2 + 3 + 4 = 9

  1. 将每个结果填充到单独的变量中。

整个计算应该在存储过程中完成。

我希望你能帮帮我!BK_

4

3 回答 3

1

如果我正确理解了您的问题,看起来您想要的是每个组的聚合,这可以通过GROUP BY子句轻松完成。

对于第一个查询,您将使用:

SELECT team, SUM(age) AS 'Sum of the team' 
FROM table 
GROUP BY team

这将给出以下结果:

Team                 Sum of the team
-------------------- ---------------
Barcelona            45
Chelsea              71
Real Madrid          46

第二个:

SELECT team, SUM(headergoal + freekickgoal) AS 'Sum of goals' 
FROM table 
GROUP BY team

这将给出这个结果:

Team                 Sum of goals
-------------------- ------------
Barcelona            4
Chelsea              9
Real Madrid          2

在您的示例数据中,您将切尔西第一部分的所需结果列为 45,但我想这只是一个错字,因为您在计算中省略了切尔西的一行?

至于把它变成一个存储过程,我可以告诉你这很容易,并让你参考文档,因为我不会为你做所有的工作......

编辑:merge into作为对评论的回应添加:

要将第二个查询的结果插入到现有表中,您可以使用INSERT如下简单语句:

INSERT table_with_team_and_goals 
SELECT team, SUM(headergoal + freekickgoal)
FROM table 
GROUP BY team

或者MERGE INTO如果您打算多次运行查询可能会更好(如果团队已经存在,则目标表将被更新):

MERGE INTO table_with_team_and_goals AS target
USING (SELECT Team, SUM(headergoal + freekickgoal) AS goals FROM table_with_goals GROUP BY team) AS source
ON target.team=source.team
 WHEN MATCHED THEN 
        UPDATE SET goals = source.goals
    WHEN NOT MATCHED THEN   
        INSERT (Team, Goals)
        VALUES (source.team, source.goals);
于 2013-10-20T22:37:36.437 回答
0
SELECT TEAM , Name, COUNT(TEAM) As GoalsPerTeam, COUNT(NAME) As GoalPerPlayer
FROM TABLE 
GROUP BY TEAM , Name

此查询将为您提供每位球员和每支球队的总进球数。

于 2013-10-20T22:41:04.717 回答
0
-- Sum age by team
SELECT Team, SUM(Age) SumAge
FROM
(
    SELECT Id, Team, Name, Age FROM
    (
        VALUES
        (102, 'Barcelona'  , 'Mike'   , 15),
        (103, 'Barcelona'  , 'Peter'  , 10),
        (104, 'Barcelona'  , 'Jacke'  , 10),
        (105, 'Barcelona'  , 'Jonas'  , 10),
        (106, 'Real Madrid', 'Michael', 20),
        (107, 'Real Madrid', 'Terry'  , 26),
        (108, 'Chelsea'    , 'James'  , 26),
        (109, 'Chelsea'    , 'Arthur' , 23),
        (110, 'Chelsea'    , 'Spence' , 22)
    ) AS X(Id, Team, Name, Age)
) X
GROUP BY Team

-- Sum goals by team
SELECT Team, SUM(HeaderGoal + FreeKickGoal) Goals
FROM
(
    SELECT Id, Team, Name, HeaderGoal, FreeKickGoal FROM
    (
        VALUES
        (104, 'Barcelona'  , 'Mike'   , 2, 1),
        (105, 'Barcelona'  , 'Peter'  , 1, 0),
        (106, 'Real Madrid', 'Michael', 0, 1),
        (107, 'Real Madrid', 'Terry'  , 0, 1),
        (108, 'Chelsea'    , 'James'  , 0, 0),
        (109, 'Chelsea'    , 'Arthur' , 2, 3),
        (110, 'Chelsea'    , 'Spence' , 4, 0)
    ) AS X(Id, Team, Name, HeaderGoal, FreeKickGoal)
) X
GROUP BY Team
于 2013-10-20T22:46:48.273 回答