Create a new table with the structure you want and then run for each match
declare @counter int declare @scoreline varchar(10) declare @matchID int set @counter = 1 set @matchID = 1 set @scoreline = '00' while (@counter <= 90) begin select @scoreline = ISNULL(scoreline,@scoreline) from scores where minute = @counter insert into filledScoreLines(matchID, minute, scoreline) select @matchID as matchID, @counter as min, @scoreline as scoreline set @counter = @counter + 1 end
declare @counter int declare @scoreline varchar(10) declare @matchID int set @counter = 1 set @matchID = 1 set @scoreline = '00' while (@counter <= 90) begin select @scoreline = ISNULL(scoreline,@scoreline) from scores where minute = @counter insert into filledScoreLines(matchID, minute, scoreline) select @matchID as matchID, @counter as min, @scoreline as scoreline set @counter = @counter + 1 end
To do this for multiple matches, simply flip all the match IDs that you have:
declare @matchID int declare getEm cursor local for select distinct matchID from scoresByMinute open getEm while (1=1) begin fetch next from getEm into @matchID if (@@fetch_status 0) begin DEALLOCATE getEm break end declare @counter int declare @scoreline varchar(10) set @counter = 1 set @scoreline = '00' while (@counter <= 90) begin select @scoreline = ISNULL(scoreline,@scoreline) from scores where minute = @counter insert into filledScoreLines(matchID, minute, scoreline) select @matchID as matchID, @counter as min, @scoreline as scoreline set @counter = @counter + 1 end end
declare @matchID int declare getEm cursor local for select distinct matchID from scoresByMinute open getEm while (1=1) begin fetch next from getEm into @matchID if (@@fetch_status 0) begin DEALLOCATE getEm break end declare @counter int declare @scoreline varchar(10) set @counter = 1 set @scoreline = '00' while (@counter <= 90) begin select @scoreline = ISNULL(scoreline,@scoreline) from scores where minute = @counter insert into filledScoreLines(matchID, minute, scoreline) select @matchID as matchID, @counter as min, @scoreline as scoreline set @counter = @counter + 1 end end
source share