试图构造一个SQL查询麻烦

时间:2012-10-24 14:03:51

标签: sql sql-server

大家好我在使用SQL查询创建所需的输出时遇到了麻烦。我是新手,我正在努力练习。 SQL查询首先为手球匹配构建表。

我如何使用SQL查询获得此输出:

Winner       | GoalsFor | GoalsAgainst | Loser       | GroupID | Venue
---------------------------------------------------------------------------
Austria      | 22       | 22           | South Korea | A       | South Korea
Cuba         | 22       | 22           | Norway      | B       | Norway
Cuba         | 22       | 22           | Norway      | D       | Norway
France       | 19       | 17           | Austria     | A       | Austria
France       | 19       | 17           | Cuba        | D       | Cuba
Iceland      | 23       | 19           | South Korea | A       | Iceland
Iceland      | 30       | 28           | Austria     | A       | Iceland
Iceland      | 37       | 18           | France      | A       | France
Norway       | 23       | 19           | France      | D       | Norway
Russia       | 23       | 19           | Norway      | D       | Russia
Russia       | 30       | 28           | Cuba        | D       | Russia
Russia       | 37       | 18           | France      | D       | France
South Korea  | 23       | 19           | France      | A       | South Korea

使用此代码:

 create table Teams (
    teamid int primary key,
    country varchar(30) unique not null,
    continent varchar(20) not null,
    strength int not null
)
go

create table Groups (
    groupid char(1) primary key,
    numteams int
)
go

create table GroupsTeams (
    groupid char(1) foreign key references Groups,
    teamid int foreign key references Teams,
    primary key (groupid, teamid)
)
go

create table Games (
   gameid int primary key identity,
   hometeam int not null foreign key references Teams,
   awayteam int not null foreign key references Teams,
   groupid char(1) not null foreign key references Groups,
   homescore int,
   awayscore int,
   unique (hometeam, awayteam, groupid)
)
go

insert into Teams values (1, 'Iceland', 'Europe', 1)
go
insert into Teams values (2, 'South Korea', 'Asia', 2)
go
insert into Teams values (3, 'Austria', 'Europe', 3)
go
insert into Teams values (4, 'France', 'Europe', 4)
go
insert into Teams values (5, 'U.S.', 'America', 5)
go
insert into Teams values (6, 'Norway', 'Europe', 6)
go
insert into Teams values (7, 'Cuba', 'America', 7)
go
insert into Teams values (8, 'Russia', 'Europe', 8)
go 
insert into Teams values (9, 'Egypt', 'Africa', 9)
go

insert into Groups values ('A', 4)
go
insert into Groups values ('B', 4)
go
insert into Groups values ('C', 2)
go
insert into Groups values ('D', 2)
go

insert into GroupsTeams values ('A', 1)
go
insert into GroupsTeams values ('A', 2)
go
insert into GroupsTeams values ('A', 3)
go
insert into GroupsTeams values ('A', 4)
go
insert into GroupsTeams values ('B', 5)
go
insert into GroupsTeams values ('B', 6)
go
insert into GroupsTeams values ('B', 7)
go
insert into GroupsTeams values ('B', 8)
go
insert into GroupsTeams values ('C', 1)
go
insert into GroupsTeams values ('C', 3)
go
insert into GroupsTeams values ('D', 2)
go
insert into GroupsTeams values ('D', 4)
go

insert into Games values (1, 2, 'A', 23, 19)
go
insert into Games values (3, 4, 'A', 17, 19)
go
insert into Games values (1, 3, 'A', 30, 28)
go
insert into Games values (2, 4, 'A', 23, 19)
go
insert into Games values (4, 1, 'A', 18, 37)
go
insert into Games values (2, 3, 'A', 22, 22)
go
insert into Games values (8, 6, 'D', 23, 19)
go
insert into Games values (7, 4, 'D', 17, 19)
go
insert into Games values (8, 7, 'D', 30, 28)
go
insert into Games values (6, 4, 'D', 23, 19)
go
insert into Games values (4, 8, 'D', 18, 37)
go
insert into Games values (6, 7, 'D', 22, 22)
go
insert into Games values (6, 7, 'B', 22, 22)
go

2 个答案:

答案 0 :(得分:2)

你可以使用这样的东西,如果得分有关系,我会加上一个值:

select 
  case when g.homescore > g.awayscore
      then ht.country
      when g.awayscore > g.homescore
      then at.country 
      else 'tie' end as winner,
  case when g.homescore > g.awayscore
      then g.homescore
      else  g.awayscore end as GoalsFor,
  case when g.homescore > g.awayscore
      then g.awayscore
      else  g.homescore end as GoalsAgainst,
  case when g.homescore > g.awayscore
      then at.country
      else  ht.country end as Loser,
  g.groupid,
  ht.country Venue
from games g
left join teams ht
  on g.hometeam = ht.teamid
left join teams at
  on g.awayteam = at.teamid
left join groups gp
  on g.groupid = gp.groupid
order by ht.country

请参阅SQL Fiddle with Demo

答案 1 :(得分:2)

http://sqlfiddle.com/#!3/4f5cf/8

select
    case
        when G.homescore > G.awayscore then T.country
        else T2.country
    end as Winner,
    case
        when G.homescore > G.awayscore then G.homescore
        else G.awayscore
    end as GoalsFor,
    case
        when G.homescore > G.awayscore then G.awayscore
        else G.homescore
    end as GoalsAgainst,
    case
        when G.homescore > G.awayscore then T2.country
        else T.country
    end as Loser,
    G.groupid as GroupID,
    T.country as Venue
from Teams as T
    inner join Games as G on G.hometeam = T.teamid
    inner join Teams as T2 on T2.teamid = G.awayteam
order by 1 asc