sql - Using DATEADD() within a cte -


i'm trying use dateadd() function in code every 'week_number_ apply date , add 7 days next week number. issues i've had if try code:

dateadd(day,(row_number() on (order leagueid)-1)*7,@startfixtureweek) fixturedate 

it displays new dates every row, , each week has 6 rows assigned them (the number of rows assigned change in future), instead of each row stating 'week_number' 1, , fixturedate '01-09-2016', states first row of 'week_number' 1 has fixturedate '01-09-2016', second row of 'week_number1' has fixturedate '08-09-2016' , on.

it should fixturedate 'week_number' 1 '01-09-2016', all of 'week_number' 2 '08-09-2016, 'week_number' 3 '15-09-2016' , on.

if try this:

dateadd(day,(row_number() on (order leagueid, week_number)-1)*7,@startfixtureweek) fixturedate, 

no results displayed

below code have dateadd() function applied (14th line bottom. virtually need working how 'week_number' works fixturedate instead.

update:

   league_teams (         -- generate unique-per-league index each team between 0         -- , (number of teams - 1) , calculate number of teams         -- if odd number generate fake team that's 0.         select teamid id,             leagueid,             row_number() on ( partition leagueid order teamid ) - 1 idx,             0 is_fake,             count(1) on ( partition leagueid ) num_teams,              (count(1) on ( partition leagueid ) % 2) num_fake         team          union         -- insert fake team if required         select null,             leagueid,             count(1),             1,             count(1),             1           team         group leagueid         having count(1) % 2  > 0     ),     cte (         -- calculate round 1 games         select              idx home_idx,             num_teams + num_fake - 1 - idx away_idx,             1 week_number,              leagueid leagueid,             num_teams num_teams,             num_fake num_fake           league_teams          2 * idx < num_teams          union          --  generate successive rounds 2 cases when away team has maximum index or otherwise.         select              case away_idx             when num_teams + num_fake - 1 home_idx + 1             else (home_idx + 1) % (num_teams + num_fake -1)             end,              case away_idx             when num_teams + num_fake - 1 away_idx             else (away_idx + 1) % (num_teams + num_fake - 1)             end,             week_number + 1,             leagueid,             num_teams,             num_fake          cte         week_number < (num_teams + num_fake - 1)     )     insert dbo.fixture     -- join cte results league_teams table convert     -- indexes used in calculation actual team ids.     select rn,            week_number,            dateadd(day,(row_number() on (order week_number)-1)*7,@startfixtureweek) weeknumber,            h.id,            a.id,            c.leagueid     (         select row_number() on (order leagueid, week_number)  rn,             t.*         (                -- duplicate results swapping home , away.                select week_number,                       home_idx,                       away_idx,                       leagueid                  cte                 union                 select week_number + num_teams + num_fake - 1,                       away_idx,                       home_idx,                       leagueid                  cte         ) t     ) c     inner join league_teams h  on ( c.home_idx = h.idx , c.leagueid = h.leagueid )     inner join league_teams on ( c.away_idx = a.idx , c.leagueid = a.leagueid )     order rn;      select * dbo.fixture     leagueid = 1   

schema:

league:

[leagueid] tinyint identity(1,1) not null primary key,  [leaguename] varchar(30) unique 

team:

[teamid] tinyint identity(1,1) not null primary key,  [teamabbreviation] char(3) unique,  [teamname] varchar(50) unique,  [leagueid] tinyint constraint fk_team_league foreign key references league(leagueid)  

fixture:

[fixtureid] int identity(1,1) not null primary key, [weeknumber] int not null, [fixturedate] date null, [hometeamid] tinyint null, [awayteamid] tinyint null, [leagueid] tinyint constraint fk_fixture_league foreign key references league(leagueid) 

data:

league:

1, 'english premiership' 2, 'english division 1' 

teams:

1, 'bcn', 'fc barcelona', 1 2, 'mad', 'real madrid', 1 3, 'ath', 'athletico madrid', 1 4, 'esp', 'espanyol', 1 5, 'man', 'manchester united', 2 6, 'bol', 'bolton', 2 7, 'che', 'chelsea', 2 8, 'ars', 'arsenal', 2 

below current output of 'fixture' table , can see dates incorrect gives different dates single week when should same date per week.

enter image description here

based on question assume in fixturedate column want show date when week begins. if case why using row_number()?
think loking for:

dateadd(week, week_number - 1, @startfixtureweek) fixturedate 

in query code appear below (i have ommitted ctes , clause query):

... insert dbo.fixture -- join cte results league_teams table convert -- indexes used in calculation actual team ids. select rn,     week_number,     dateadd(week, week_number - 1, @startfixtureweek) fixturedate,     h.id,     a.id,     c.leagueid ( ... 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -