sql - How to insert into a table from a CTE -
i have table selects 11 players out of 20 (based on positions) each match. i’ve done created new table know ‘fixtureplayer’ store in playerid , fixtureid table.
i not sure on how can insert pl.playerid , pl.fixtureid 'playerfixture' table calling select. should do?
with pl (select distinct p.playerid ,p.position ,case when p.teamid = 0 0 else p.playerweighting end playerweighting ,abs(checksum(newid())) % 10 + 1 form ,t.teamid dbo.fixture f inner join dbo.league l on f.leagueid = l.leagueid inner join dbo.team t on l.leagueid = t.leagueid inner join dbo.player p on t.teamid = p.teamid f.weeknumber = 1) ,po (select *, row_number() over(partition pl.teamid, pl.position order newid()) rnk pl) ,total (select teamid ,sum(playerweighting) teamweight ,sum((playerweighting / 10) * form + playerweighting) finalteamweight po (po.position = 'gk' , po.rnk = 1) or (po.position = 'df' , po.rnk <= 4) or (po.position = 'mf' , po.rnk <= 4) or (po.position = 'fw' , po.rnk <= 2) group po.teamid
create procedure [dbo].[matchday_insert] set nocount on begin select [teamid], [teamweight], [form], [finalteamweight] teamweighting order teamid select * player end
playerfixture:
create table [dbo].[playerfixture] ( [fixtureid] int not null primary key, [playerid] int null )
the question unclear think wondering how use same cte
twice? once inserting data dbo.playerfixture
, once selecting data?
the way temp table
if object_id('tempdb..#tblname') not null begin drop table #tblname end ;with cte ( ....cte definition ) insert #tblname (col1,....) select col1 cte insert dbo.playerfixture (fixtureid, playerid) select fixtureid ,playerid #tblname select * #tblname
and if meaning how data teh 3 cte's have defined above write insert dbo.playerfixture select
directly after cte definition though doesn't want cte's themselves.
Comments
Post a Comment