I hope this is to do away with that denormalized table
But I'd think this is more efficient. Of course you'll have to extend out the remaining columns (4-12) in the 2 case statements.set nocount onset ansi_warnings offcreate table #tblTest (Qty1 int, Qty2 int, Qty3 int, Dol1 money, Dol2 money, Dol3 money)insert #tblTest select 1,2,3,10,20,30create table #test (period int, Qty int, Dol money)insert #test (period)select 1 unionselect 2 unionselect 3select * from #tblTestselect * from #testupdate a set a.Qty = b.Qty ,a.Dol = b.Dolfrom #test ajoin (--derived table using tblTest cross joined with a numbers table (12 months) select m as period ,Qty = case when m=1 then Qty1 when m=2 then Qty2 when m=3 then Qty3 end ,Dol = case when m=1 then Dol1 when m=2 then Dol2 when m=3 then Dol3 end from #tblTest cross join ( select 1 m union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 ) months ) b on b.period = a.periodprint 'After update'select * from #testgodrop table #tblTestdrop table #testOUTPUT:Qty1 Qty2 Qty3 Dol1 Dol2 Dol3 ----------- ----------- ----------- --------------------- --------------------- --------------------- 1 2 3 10.0000 20.0000 30.0000period Qty Dol ----------- ----------- --------------------- 1 NULL NULL2 NULL NULL3 NULL NULLAfter updateperiod Qty Dol ----------- ----------- --------------------- 1 1 10.00002 2 20.00003 3 30.0000
Be One with the OptimizerTG