SQL Server:选择并计数

时间:2015-05-30 03:00:54

标签: sql sql-server

我有3张桌子:

> HockeyTeam with columns: ID and Name 
> HockeyGame with columns: ID,Team1ID and Team2ID (both linked to HockeyTeam ID) 
> GameScores with columns: ID, GameID, Team1Score, Team2Score

HockeyTeam

ID   Name
1    Monkeys
2    Beavers
3    Dolphins
4    Snakes

GameScore

ID   GameID   Team1Score   Team2Score
1    1        3            2
2    2        4            0
3    3        2            3
4    4        2            4

HockeyGame

ID   StartTime   Team1ID   Team2ID
1    2/6/2015    1         2
2    2/13/2015   3         4
3    2/20/2015   2         4
4    2/27/2015   1         3

我需要显示每个团队的总目标。团队可以同时位于Team1IDTeam2ID。这是我到目前为止所得到的:

SELECT  
    ht.Name, 
    SUM(SELECT (gs.Team1Score + gs.Team2Score) 
        FROM GameScores as gs
            INNER JOIN HockeyGame as hg
            INNER JOIN  HockeyTeam  as ht
            ON ht.ID = hg.Team1ID OR ht.ID = ht.Team2ID
            ON gs.GameID = hg.ID
FROM 
    HockeyTeam  as ht

3 个答案:

答案 0 :(得分:5)

让我们采取如下结构:

create table team (id int, name varchar(20));
insert into team values (1, 'H1'), (2, 'H2'), (3, 'H3');

create table game (id int, team1id int, team2id int);
insert into game values (11, 1, 2), (12, 1, 3), (13, 2, 3);

create table score (
  id int,
  gameid int,
  team1score int,
  team2score int
);
insert into score values 
(21, 11, 5, 2), 
(22, 12, 2, 5), 
(23, 13, 0, 2);  

显示游戏结果(尚未回答)

-- show readable game results
select
  s.gameid,
  t1.name as team1,
  t2.name as team2,
  team1score,
  team2score
from score s
inner join game g on s.gameid = g.id
inner join team t1 on g.team1id = t1.id
inner join team t2 on g.team2id = t2.id;

数据如下所示:

gameid  team1  team2  team1score  team2score
11      H1     H2     5           2
12      H1     H3     2           5
13      H2     H3     0           2

让我们现在得分(答案)

-- show score by team 
select
  t.name,
  sum(score) as goals
from team t

left join 
(
  -- get score of team1
  select
    t1.id as teamid,
    sum(team1score) as score
  from score s
  inner join game g on s.gameid = g.id
  inner join team t1 on g.team1id = t1.id
  group by t1.id

  union all

  -- get score of team2 and combine it with results from team1
  -- this is because team2 can be team1 for some games
  select
    t2.id as teamid,
    sum(team2score) as score
  from score s
  inner join game g on s.gameid = g.id
  inner join team t2 on g.team2id = t2.id
  group by t2.id
) t1 on t.id = t1.teamid

group by t.name

结果将如下所示:

Name  Goals
H1    7
H2    2
H3    7

示例:http://sqlfiddle.com/#!9/aa3cc/15 虽然该示例适用于MySQL(因为SQL Server Fiddle正在执行),但SQL语句对SQL Server仍然有效。

答案 1 :(得分:2)

您可以在不NewSession的情况下执行此操作:

unions

答案 2 :(得分:1)

作为替代方案:

select t.name,
    SUM(CASE
        WHEN gt.teamSeq = 1 then s.team1Score
        WHEN gt.teamSeq = 2 then s.team2Score
    END) As Scores
from 
    team t 
    LEFT JOIN
    (select g.id,
        case 
            when g.team1id = t.id then g.team1id
            when g.team2id = t.id then g.team2id
        end as teamid,
        case 
            when g.team1id = t.id then 1
            when g.team2id = t.id then 2
        end as teamSeq
    from game g
        cross join
        team t) gt
    ON t.id = gt.teamid
    LEFT JOIN
    score s
    ON gt.id = s.gameid
where
    gt.teamid IS NOT NULL 
group by
    t.name