2017-04-24 102 views
1

我有本赛季所有比赛的积分表的MSSQL查询。SQL足球积分表最后5场比赛

该数据库由一个表和一个表的列名组成;

Div (League name), Date, HomeTeam, AwayTeam, FTHG(FullTimeHomeGoal), FTAG (FullTimeAwayGoal)... 

表结构

Div (nvarchar) Date (datetime) Hometeam (nvarchar) Awayteam (nvarchar) Fthg (float) Fthg (float)

我该怎么办的最后5场比赛/游戏积分榜?

select 
    team, 
    count(*) MP, 
    count(case when fthg > ftag then 1 end) W, 
    count(case when fthg = ftag then 1 end) D, 
    count(case when fthg < ftag then 1 end) L, 
    sum(fthg) GF, 
    sum(ftag) GA, 
    sum(fthg) - sum(ftag) GD, 
    sum(case when fthg > ftag then 3 else 0 end + case when fthg = ftag then 1 else 0 end) Pts 
    from (
    select Div, hometeam team, fthg, ftag, hthg, htag from Matches 
     union all 
    select Div, awayteam team, ftag, fthg, htag, hthg from Matches 
    ) a 
    where div='E0' 
group by team 
order by Pts desc 

查询结果:

team  MP W D L GF GA GD Pts 
Chelsea  32 24 3 5 65 27 38 75 
Tottenham 32 21 8 3 68 22 46 71 
Liverpool 33 19 9 5 69 40 29 66 
Man City 32 19 7 6 63 35 28 64 
Man United 31 16 12 3 48 24 24 60 
Arsenal  31 17 6 8 63 40 23 57 
Everton  33 16 9 8 60 37 23 57 
West Brom 33 12 8 13 39 42 -3 44 
Watford  32 11 7 14 37 52 -15 40 
Southampton 31 11 7 13 37 40 -3 40 
Stoke  33 10 9 14 37 48 -11 39 
Leicester 32 10 7 15 41 53 -12 37 
West Ham 33 10 7 16 44 59 -15 37 
Burnley  33 10 6 17 33 47 -14 36 
Bournemouth 33 9 8 16 45 63 -18 35 
Crystal Pa 32 10 5 17 44 52 -8 35 
Hull  33 8 6 19 34 67 -33 30 
Swansea  33 8 4 21 37 68 -31 28 
Middlesbr 32 4 12 16 23 39 -16 24 
Sunderland 32 5 6 21 26 58 -32 21 

Sample data div date hometeam awayteam fthg ftag E0 2017-04-17 00:00:00.000 Middlesbrough Arsenal 1 2 E0 2017-04-16 00:00:00.000 Man United Chelsea 2 0 E0 2017-04-16 00:00:00.000 West Brom Liverpool 0 1 E0 2017-04-15 00:00:00.000 Crystal Palace Leicester 2 2 E0 2017-04-15 00:00:00.000 Everton Burnley 3 1 ..... ...

+0

是不是有任何日期时间列? – VDK

+0

您是否尝试过Top 5 Date? –

+0

@ mehtat_90日期字段不适用于考试。 我认为计算团队比赛号码更为正确 –

回答

0

由于我的编辑杰里米真正的答案被拒绝后,我张贴我自己的,以信誉@JeremyReal(也upvoted)。

with table_a as (
    select Div, date, hometeam team, fthg, ftag, hthg, htag from Matches 
     union all 
    select Div, date, awayteam team, ftag, fthg, htag, hthg from Matches 
    ) 
,table_b as (
select * from (
    select a.* 
    ,row_number() over (partition by a.Div, a.team order by a.date desc) as row_num 
    from table_a a) x 
where row_num <= 5) 

select 
    Div, 
    team, 
    count(*) MP, 
    count(case when fthg > ftag then 1 end) W, 
    count(case when fthg = ftag then 1 end) D, 
    count(case when fthg < ftag then 1 end) L, 
    sum(fthg) GF, 
    sum(ftag) GA, 
    sum(fthg) - sum(ftag) GD, 
    sum(case when fthg > ftag then 3 else 0 end + case when fthg = ftag then 1 else 0 end) Pts 
    from table_b 
    where div='E0' --remove this line to show all divisions. 
group by Div, team 
order by Div, Pts desc 

与杰里米的回答相比,这增加了一个别名(x)子查询的table_b CTE。它还将Div添加到该子查询中的分区以及主查询中的group by

我认为在主要查询中包含Div的是可能是是在不同比赛中运行的同名球队。或者甚至原始数据可能包括“杯赛”和联赛的比赛,而且这两者不应该混为一谈。根据你的数据结构,你甚至可能在你的表中有多个季节的数据,这也需要处理。

1

我认为这是可行的。

with table_a as (
    select Div, date, hometeam team, fthg, ftag, hthg, htag from Matches 
     union all 
    select Div, date, awayteam team, ftag, fthg, htag, hthg from Matches 
    ) 
,table_b as (
select * from (
    select a.* 
    ,row_number() over (partition by a.team order by a.date desc) as row_num 
    from table_a a) 
where row_num <= 5) 

select 
    team, 
    count(*) MP, 
    count(case when fthg > ftag then 1 end) W, 
    count(case when fthg = ftag then 1 end) D, 
    count(case when fthg < ftag then 1 end) L, 
    sum(fthg) GF, 
    sum(ftag) GA, 
    sum(fthg) - sum(ftag) GD, 
    sum(case when fthg > ftag then 3 else 0 end + case when fthg = ftag then 1 else 0 end) Pts 
    from table_b 
    where div='E0' 
group by team 
order by Pts desc 
+0

谢谢,但只有match_id不存在表 –

+0

@RobinVlaar好的,删除它。这不是必需的,但最初认为这是必需的。现在应该工作。 –

+0

它不工作。没有任何结果..空 –