dm 表 列为
zh yyear  mmonth  xun A_3_1  A_3_2 A_3_3   B_4_1  B_4_2 B_4_3   C_4_1  C_4_2 C_4_3  
 
共13列 
update   a   
a.A_3_1   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=1 and a.xun=1
update   a   
a.A_3_2   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=1 and a.xun=2update   a   
a.A_3_3   =   b.total 
from   dm   a   dn   b  
where   a.yyear=b.yyear   and   a.mmonth=1 and a.xun=3
update   a   
a.B_4_1   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=2 and a.xun=1
update   a   
a.B_4_2   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=2 and a.xun=2update   a   
a.B_4_3   =   b.total 
from   dm   a   dn   b  
where   a.yyear=b.yyear   and   a.mmonth=2 and a.xun=3
update   a   
a.C_5_1   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=3 and a.xun=1
update   a   
a.C_5_2   =   b.total 
from   dm   a   dn   b 
where   a.yyear=b.yyear   and   a.mmonth=3 and a.xun=2update   a   
a.B_5_3   =   b.total 
from   dm   a   dn   b  
where   a.yyear=b.yyear   and   a.mmonth=3 and a.xun=3谢谢

解决方案 »

  1.   

    update       a       
    a.A_3_1       =       b.total   
    from       dm       a       dn       b   
    where       a.yyear=b.yyear       and       a.mmonth=1   and   a.xun=1 
    update       a       
    a.A_3_2       =       b.total   
    from       dm       a       dn       b   
    where       a.yyear=b.yyear       and       a.mmonth=1   and   a.xun=2 update       a       
    a.A_3_3       =       b.total   
    from       dm       a       dn       b     
    where       a.yyear=b.yyear       and       a.mmonth=1   and   a.xun=3 
    ---
    三个并一个
    update       a       
    a.A_3_1       =  case when a.xun=1 then   b.total else  0 end ,
    a.A_3_2       =  case when a.xun=2 then   b.total else  0 end ,
    a.A_3_3       =  case when a.xun=3 then   b.total else  0 end 
    from       dm       a       dn       b     
    where       a.yyear=b.yyear       and       a.mmonth=1
      

  2.   

     假如 C_4_1     C_4_2   C_4_3  列后有 D_5_1     D_5_2   D_5_3 ......
    等多列,如何用循环来实现,谢谢!
      

  3.   

    update a
    set A_3_1=case when a.mmonth=1 and  a.xun=1 then b.total else a.A_3_1 end,--条件不满足时,不改变
    A_3_2=case when a.mmonth=1 and  a.xun=2 then b.total else a.A_3_2 end,
    A_3_3=case when a.mmonth=1 and  a.xun=3 then b.total else a.A_3_3 end,--条件不满足时,不改变
    A_4_1=case when a.mmonth=2 and  a.xun=1 then b.total else a.A_4_1 end,
    A_4_2=case when a.mmonth=2 and  a.xun=2 then b.total else a.A_4_2 end,
    A_4_3=case when a.mmonth=2 and  a.xun=3 then b.total else a.A_4_3 end,
    A_5_1=case when a.mmonth=3 and  a.xun=1 then b.total else a.A_5_1 end,
    A_5_2=case when a.mmonth=3 and  a.xun=2 then b.total else a.A_5_2 end,
    A_5_3=case when a.mmonth=3 and  a.xun=3 then b.total else a.A_5_3 end
    from 
    dm a
    join 
    dn b on a.yyear=b.yyear