3

こんにちは、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
4

2 に答える 2

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 を参照してください

于 2012-10-24T14:19:13.467 に答える
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
于 2012-10-24T14:20:51.077 に答える