SQL选择查询

时间:2017-03-20 05:58:15

标签: sql

请求帮助我选择( Tot-Goals,Tot-Goals-against,每个团队的目标差异)我正在使用名为[Match Details]的表,而字段是

*Tour-Id, Match Id, Team-Id, Goals*
    1       1          1       1
    1       1          2       1
    1       1          1       1
    1       2          1       1
    1       2          3       1

我正在使用查询

**参数TourLong;

SELECT Team-Id, SUM(Goals) as goals
FROM [Match Details]
WHERE [tourid] = Tour
GROUP BY club, tourid;

我正在获得团队的目标,但不是针对目标

1 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

create table #MatchDetails ( tour_id int,macth_id int ,team_id int,goal int)

insert into #MatchDetails values(1,1,1,1);
insert into #MatchDetails values(1,1,2,1);
insert into #MatchDetails values(1,2,3,6);
insert into #MatchDetails values(1,2,2,1);
insert into #MatchDetails values(1,3,1,4);
insert into #MatchDetails values(1,3,3,1);

    select  m1.team_id as team_id,SUM(m1.goal) as totalgoal,SUM(m2.goal) as totalgoalagainst,SUM(m1.goal)-SUM(m2.goal) as totaldifference   from #MatchDetails m1    inner join #MatchDetails m2 
      on m1.macth_id=m2.macth_id and m1.team_id!=m2.team_id
    Where m1.tour_id=1
    group by  m1.team_id
drop table #MatchDetails;

输出

 team_id    totalgoal   totalgoalagainst    totaldifference
    1         5          2                   3
    2         2          7                  -5
    3         7          5                   2

让我知道我是否可以提供更多帮助。