Witam Panowie mam problem
Chce stworzyć tabele przedstawiająca mecze piłki nożnej która uwzględniała by ostatnie 5 meczy każdej z drużyn i nie mogę sobie dać rady.
Próbowałem przez poniższe querry z row number <5 i niestety nie działa . Byłbym wdzięczny za jakieś podpowiedzi
chciałem zrobić podzapytanie i pivotem odwrócić ale tez nie za bardzo wiem jak się do tego zabrać ??
zapytanie
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
wynik
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
tabela mecze
ID Country League Season data Time Home Away HG AG Res
43086 France Ligue 1 2012/2013 10.08.2012 21:00:00.0000000 Montpellier Toulouse 1 1 D
43087 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Evian Thonon Gaillard Bordeaux 2 3 A
43088 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Nancy Brest 1 0 H
43089 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Nice Ajaccio 0 1 A
43090 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Paris SG Lorient 2 2 D
43091 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Rennes Lyon 0 1 A
43092 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Sochaux Bastia 2 3 A
43093 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 St Etienne Lille 1 2 A
43094 France Ligue 1 2012/2013 11.08.2012 21:00:00.0000000 Troyes Valenciennes 0 1 A
43095 France Ligue 1 2012/2013 12.08.2012 21:00:00.0000000 Reims Marseille 0 1 A
43096 France Ligue 1 2012/2013 17.08.2012 21:00:00.0000000 Lille Nancy 1 1 D
43097 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Bastia Reims 2 1 H
43098 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Brest Evian Thonon Gaillard 1 0 H
43099 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Lorient Montpellier 2 1 H
43100 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Lyon Troyes 4 1 H
43101 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Toulouse St Etienne 2 1 H
43102 France Ligue 1 2012/2013 18.08.2012 21:00:00.0000000 Valenciennes Nice 0 0 D
43103 France Ligue 1 2012/2013 19.08.2012 21:00:00.0000000 Ajaccio Paris SG 0 0 D
43104 France Ligue 1 2012/2013 19.08.2012 21:00:00.0000000 Bordeaux Rennes 1 0 H
43105 France Ligue 1 2012/2013 19.08.2012 21:00:00.0000000 Marseille Sochaux 2 0 H
43106 France Ligue 1 2012/2013 24.08.2012 21:00:00.0000000 Evian Thonon Gaillard Lyon 1 1 D
43107 France Ligue 1 2012/2013 25.08.2012 21:00:00.0000000 Nancy Toulouse 0 1 A
43108 France Ligue 1 2012/2013 25.08.2012 21:00:00.0000000 Nice Lille 2 2 D