假设我有一个包含3个表的数据库:
玩家参加锦标赛。名为Game的表记录每个玩家在不同比赛中的得分)
我想创建一个如下所示的视图:
torunament_name Winner highest_score
Tournament_1 Jones 300
Tournament_2 White 250
我尝试了不同的aproaches,但我对sql相当新(并且alsoto这个论坛) 我尝试使用union all子句,如:
select * from (select "Id_player", avg("score") as "Score" from
"Game" where "Id_tournament" = '1' group by "Id_player" order by
"Score" desc) where rownum <= 1
union all
select * from (select "Id_player", avg("score") as "Score" from
"Game" where "Id_tournament" = '2' group by "Id_player" order by
"Score" desc) where rownum <= 1;
和ofc它有效,但每当锦标赛发生时,我都必须手动添加一个选择语句,并使用Id_torunament = nextvalue
编辑: 因此,让我们说id为1的玩家在锦标赛中获得50分,玩家2获得40分,玩家1获胜,因此该表应该只显示玩家1作为获胜者(或者如果其可能的2或更多玩家,如果它的平局)这场比赛。下一行显示第二场比赛的获胜者。我不认为我会在同一场比赛中为一名球员投入多场比赛,但如果我愿意的话,可能会从他的所有得分中算出平均值。
EDIT2: 创建表脚本:
create table players
(id_player numeric(5) constraint pk_id_player primary key, name
varchar2(50));
create table tournaments
(id_tournament numeric(5) constraint pk_id_tournament primary key,
name varchar2(50));
create table game
(id_game numeric(5) constraint pk_game primary key, id_player
numeric(5) constraint fk_id_player references players(id_player),
id_tournament numeric(5) constraint fk_id_tournament references
tournaments(id_tournament), score numeric(3));
最终编辑: 好的,如果有人想知道我使用的是Jorge Campos脚本,请稍微更改一下它就可以了。谢谢大家的帮助。不幸的是我还不能提出意见,所以我只能通过发帖来表示感谢。下面是最后的剧本:
select
t.name,
p.name as winner,
g.score
from
game g inner join tournaments t
on g.id_tournament = t.id_tournament
inner join players p
on g.id_player = p.id_player
inner join
(select g.id_tournament, g.id_player,
row_number() over (partition by t.name order by
score desc) as rd from game g join tournaments t on
g.id_tournament = t.id_tournament
) a
on g.id_player = a.id_player
and g.id_tournament = a.id_tournament
and a.rd=1
order by t.name, g.score desc;
答案 0 :(得分:0)
假设你想要什么&#34;在每场比赛中显示每个球员的高分&#34; 您的查询将如下所示在MS Sql server
中select
t.name as tournament_name,
p.name as Winner,
Max(g.score) as [Highest_Score]
from Tournmanents t
Inner join Game g on t.id_tournament=g.id_tournament
inner join Players p on p.id_player=g.id_player
group by
g.id_tournament,
g.id_player,
t.name,
p.name
答案 1 :(得分:0)
根据您使用的RDBM,可以简化此查询。
select
t.name,
p.name as winner,
g.score
from
game g inner join tournaments t
on g.id_tournament = t.id_tournament
inner join players p
on g.id_player = p.id_player
inner join
(select id_tournament,
id_player,
row_number() over (partition by t.name order by score desc) as rd
from game
) a
on g.id_player = a.id_player
and g.id_tournament = a.id_tournament
and a.rd=1
order by t.name, g.score desc
答案 2 :(得分:0)
请检查这是否适用于您
SELECT tournemntData.id_tournament ,
tournemntData.name ,
dbo.Players.name ,
tournemntData.Score
FROM dbo.Game
INNER JOIN ( SELECT dbo.Tournaments.id_tournament ,
dbo.Tournaments.name ,
MAX(dbo.Game.score) AS Score
FROM dbo.Game
INNER JOIN dbo.Tournaments ONTournaments.id_tournament = Game.id_tournament
INNER JOIN dbo.Players ON Players.id_player = Game.id_player
GROUP BY dbo.Tournaments.id_tournament ,
dbo.Tournaments.name
) tournemntData ON tournemntData.id_tournament =Game.id_tournament
INNER JOIN dbo.Players ON Players.id_player = Game.id_player
WHERE tournemntData.Score = dbo.Game.score