Author |
Topic |
nirene
Yak Posting Veteran
98 Posts |
Posted - 2014-08-05 : 02:34:02
|
Dear All,Help me by giving a Dynamic query statement for the below mentioned tablesTargetCocode-Brcode-Loccode-Month-Year-TargetC1 -B1 -L1 -4 -2014-100C1 -B1 -L1 -5 -2014-120C1 -B1 -L1 -6 -2014-140TargetFormatCocode-Brcode-Loccode-Apr_T-May_T-Jun_TC1 -B1 -L1 -0 -0 -0Now I'm updating this TargetFormatUpdate TargetFormat Set Apr_T=T.Target from TargetFormat TF,Target T Where TF.Cocode=T.Cocode and TF.Brcode=T.Brcode and TF.Loccode=T.Loccode and T.Month=4 and T.Year=2014And so onAfter UpdateTargetFormatCocode-Brcode-Loccode-Apr_T-May_T-Jun_TC1 -B1 -L1 -100 -120 -140Select Left(DateName(month,DateAdd(month,Month,0)-1),3)+'_T' from Target Taking the above as example, can a update be done in single stroke using dynamic sql.Thanks in advance.Nirene |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-08-05 : 08:35:50
|
Here's one way without dynamic sql:declare @target table (Cocode char(2), Brcode char(2), Loccode char(2), [Month] int, [Year] int, target int);declare @TargetFormat table (Cocode char(2), Brcode char(2), Loccode char(2), Apr_T int, May_T int, Jun_T int);insert into @target values('C1', 'B1', 'L1', 4, 2014, 100),('C1', 'B1', 'L1', 5, 2014, 120),('C1', 'B1', 'L1', 6, 2014, 140);insert into @TargetFormat values('C1', 'B1', 'L1', 0, 0 ,0);with targetupvt as ( select Cocode, Brcode, Loccode, case month when 'Apr_T' then 4 when 'May_T' then 5 when 'Jun_T' then 6 end as month, target from @TargetFormat unpivot (target for month in (Apr_T, May_T, Jun_T)) u),targetbymonth as ( select ca.* from @target T cross apply ( select tf.month, t.target from targetupvt tf where TF.Cocode=T.Cocode and TF.Brcode=T.Brcode and TF.Loccode=T.Loccode and tf.month = t.Month and T.Year=2014 ) ca)update @TargetFormatset Apr_T = p.[4], May_T = p.[5], Jun_T = p.[6]from targetbymonthpivot(max(target) for month in ([4],[5],[6])) pselect * from @targetFormat |
 |
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2014-08-08 : 01:19:40
|
Hai gbritton,Thanks for your post and sorry for the delayed reply.Forgot to mention in my previous post that achievement also to be added to target.Sample Datadeclare @target table (Cocode char(2), Brcode char(2), Loccode char(2), [Month] int, [Year] int, target int,achievement int);declare @TargetFormat table (Cocode char(2), Brcode char(2), Loccode char(2), Apr_T int,Apr_A int, May_T int,May_A int, Jun_T int,Jun_A int);insert into @target values('C1', 'B1', 'L1', 4, 2014, 100,80)insert into @target values('C1', 'B1', 'L1', 5, 2014, 120,110)insert into @target values('C1', 'B1', 'L1', 6, 2014, 140,120)insert into @TargetFormat values('C1', 'B1', 'L1', 0,0,0,0,0,0);Expected ResultCocode,Brcode,Loccode,Apr_T,Apr_A,May_T,May_A,Jun_T,Jun_AC1 ,B1 ,L1 ,100 ,80 ,120 ,110 ,140 ,120RegardsNirene |
 |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-08-08 : 10:24:25
|
Try to work with my solution and modify it to do what you want |
 |
|
|
|
|