musisz zrezygnować z id tygodnia tylko pobierać tabelę na odpowiednią datę, napisałbym funkcje pomocniczą:
CREATE FUNCTION dbo.tabelawynikow(@d datetime)
RETURNS TABLE
AS
return
select
ROW_NUMBER() OVER( ORDER BY (sum(case when hg > ag then 3 else 0 end + case when hg = ag then 1 else 0 end )) desc,(sum(hg) - sum(ag))desc, (sum(hg)) desc ) AS Pozycja ,
team,
sum(case when hg is null then 0 else 1 end ) 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
(
SELECT country,
Season,
data,
home team,
hg,
ag
FROM MECZE m
WHERE time <= @d
UNION
SELECT country,
Season,
data,
away team,
ag,
hg
FROM MECZE
WHERE time <= @d) as dt
group by country ,team
i pobrał daty dla których je wyświetlać:
declare @dtstart as datetime
declare @dtend as datetime
select @dtstart= min (data_start),@dtend=max(data_end) from tygodnie where id_week between @d and @d2
--to zapytanie zwraca daty na które musisz puścić funkcje tabelawynikow
select distinct data from mecze where data between @dtstart and @dtend order by 1
Nie napisze Ci kursora bo uważam że to powinna już przejać aplikacja, a nie być procesowane po stronie bazy, jak sie uprzesz to poczytaj
to podejście zadziała nawet jak mecze będą codziennie