I have a MSSQL query for point table of all matches of this season.
The database consists of one table and table column name is;
Div (League name), Date, HomeTeam, AwayTeam, FTHG(FullTimeHomeGoal), FTAG (FullTimeAwayGoal)...
Table structure
Div (nvarchar)
Date (datetime)
Hometeam (nvarchar)
Awayteam (nvarchar)
Fthg (float)
Fthg (float)
How can i do points table of last 5 matches/games ?
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
Query results:
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 :(得分: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
答案 1 :(得分:0)
由于我对Jeremy Real的回答被拒绝了,所以我发布了自己的回复,同时归功于@JeremyReal(也是赞成)。
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
与Jeremy的回答相比,这为x
CTE的子查询添加了别名(table_b
)。它还会将Div
添加到该子查询中的分区以及主查询中的group by
。
我在主要查询中加入Div
的想法是,可以是在不同比赛中同名的球队。或者甚至原始数据可能包括“杯赛”比赛以及联赛事件,并且两者不应该混淆。根据您的数据结构,您甚至可能在表中有多个季节的数据,这也需要处理。