我有类似的问题:SQL Soccer Points Table Last 5 Matches
可以使表格像那个简单的表格吗?我需要使用数据透视表吗?
[]
每个团队(如表单)的5个最后一场比赛
我制作表格的代码:
with table_a as (
select country,Season, data, home team, hg, ag from MECZE
union all
select country,Season, data, away team, ag, hg from MECZE
)
,table_b as (
select * from (
select a.*
,row_number() over (partition by a.country ,a.team order by a.data desc) as row_num
from table_a a) X
where row_num <= 333 )
select
team,
count(*) MP,
count(case when hg > ag then 1 end) W,
count(case when hg = ag then 1 end) D,
count(case when hg < ag then 1 end) L,
sum(hg) GF,
sum(ag) GA,
sum(hg) - sum(ag) GD,
sum(case when hg > ag then 3 else 0 end + case when hg = ag then 1 else 0 end) Pts
from table_b
where country='france' and Season ='2012/2013'
group by country, team
order by country, Pts desc
有结果:
team MP W D L GF GA GD Pts
-------------------------------------------
Paris SG 38 25 8 5 69 23 46 83
Marseille 38 21 8 9 42 36 6 71
Lyon 38 19 10 9 61 38 23 67
Nice 38 18 10 10 57 46 11 64
St Etienne 38 16 15 7 60 32 28 63
Lille 38 16 14 8 59 40 19 62
Bordeaux 38 13 16 9 40 34 6 55
Lorient 38 14 11 13 57 58 -1 53
Montpellier 38 15 7 16 54 51 3 52