我有本賽季所有比賽的積分表的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 ..... ...
是不是有任何日期時間列? – VDK
您是否嘗試過Top 5 Date? –
@ mehtat_90日期字段不適用於考試。 我認爲計算團隊比賽號碼更爲正確 –