2017-04-24 61 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

它不工作。沒有任何結果..空 –