hanchilicious
Programmer
Hi,
I have a script which, depending on the week the job is bring run, will update a different column.
As you can see, this code will update a moving column, depending on the value of loadweek.
However, the business has decided that they need to populate ALL historical values, not just one. So, if LoadWeek = 10, then instead of just updating Week09, column Week01 through Week09 should be updated.
What is the best way to achieve this, please?
Thanks
I have a script which, depending on the week the job is bring run, will update a different column.
Code:
declare @varcol varchar(10)
declare @ex varchar (200)
SELECT @varcol =
(CASE
WHEN LoadWeek = 1 then 'week01'
WHEN LoadWeek = 2 then 'week02'
WHEN LoadWeek = 3 then 'week03'
WHEN LoadWeek = 4 then 'week04'
WHEN LoadWeek = 5 then 'week05'
WHEN LoadWeek = 6 then 'week06'
WHEN LoadWeek = 7 then 'week07'
WHEN LoadWeek = 8 then 'week08'
WHEN LoadWeek = 9 then 'week09'
WHEN LoadWeek = 10 then 'week10'
WHEN LoadWeek = 11 then 'week11'
WHEN LoadWeek = 12 then 'week12'
WHEN LoadWeek >= 13 then 'week13'
END)
from Bk_TSR_Forecast
select @ex = 'update tsrdb
set ' + @varcol + ' = b.' + @varcol +
' from tsr_forecast b
where tsrdb.trueid = b.customernumber
and tsrdb.trueppn = b.product'
EXEC (@ex)
However, the business has decided that they need to populate ALL historical values, not just one. So, if LoadWeek = 10, then instead of just updating Week09, column Week01 through Week09 should be updated.
What is the best way to achieve this, please?
Thanks