原表总表:
年      月   部门     A金额   B金额  C金额  D金额
2012    8    一部     100     200     0     0
2012    8    二部     50      150     0     0分表1:
年      月   部门     C金额
2012    8    一部     200
2012    8    三步     100分表2:
年      月   部门     D金额
2012    8    二部     300
2012    8    三步     90
2012    8    四部     120求一更新语句,把总表更新为以下数据:年      月   部门     A金额   B金额  C金额  D金额
2012    8    一部     100     200     200     0
2012    8    二部     50      150     0       300
2012    8    三部     0       0       100     90
2012    8    四部     0       0        0      120谢谢

解决方案 »

  1.   

    declare @test table(year int,month int,department nvarchar(5),A int,B int,C int,D int)
    insert into @test
    select 2012, 8, N'一部', 100, 200, 0, 0 union all
    select 2012, 8, N'二部', 50, 150, 0, 0
    declare @tab1 table(year int,month int,department nvarchar(5),C int)
    insert into @tab1
    select 2012, 8, N'一部', 200 union all
    select 2012, 8, N'三部', 100
    declare @tab2 table(year int,month int,department nvarchar(5),D int)
    insert into @tab2
    select 2012, 8, N'二部', 300 union all
    select 2012, 8, N'三部', 90 union all
    select 2012, 8, N'四部', 120select t.year,t.month,t.department,sum(A) A,sum(B) B,sum(C) C,sum(D) D from
    (
    select * from @test
    union all
    select year,month,department,cast('' as int) A,cast('' as int) B,C,cast('' as int) D from @tab1
    union all
    select year,month,department,cast('' as int) A,cast('' as int) B,cast('' as int) C,D from @tab2
    ) t
    group by t.year,t.month,t.department
    /*
    year        month       department A           B           C           D
    ----------- ----------- ---------- ----------- ----------- ----------- -----------
    2012        8           二部         50          150         0           300
    2012        8           三部         0           0           100         90
    2012        8           四部         0           0           0           120
    2012        8           一部         100         200         200         0
    */
      

  2.   

    很好,很强大.我本来还想着是否可以用merge
      

  3.   

    你这种表结构,直接补足每个列,字段相同直接用union 连接就可以了一楼仁兄出手好快
      

  4.   

    分2次更新..
    update a set C=b.C
    from @test a
    join @tab1 b on a.year=b.year and a.month=b.month and a.department=b.department另一个自己看着写吧
      

  5.   


    关键不止是Update,还要判断要不要Insert吧
      

  6.   

    确实除了update,还要insert的,比较麻烦。
    如果你的数据量不大的话,先按我之前给的方法,把结果放入一个临时表,然后把总体的数据truncate了,再把临时表的数据insert进去
      

  7.   


    我也想到这个方法,实际情况是总表大概有80多个金额字段。后续两个Select写起来有点那个
      

  8.   

    如果你的分表名和金额字段的名称是有规律的话,还可以用动态sql来做,如果没规律的话,那就只能一个一个update和insert了
      

  9.   

     /**(25)
     *    program:CSDN问答练习
     *   username:zsq
     * cteatetime:2012-8-23
     *       心得:?
     *       配置:sql server 2008
     */ DECLARE @zong TABLE ( [YEAR] INT ,[month] INT ,WORK NVARCHAR(20),a_money MONEY,b_money MONEY,c_money MONEY,d_money MONEY)
    DECLARE @fen_a TABLE ([year] INT ,[month] INT ,WORK NVARCHAR(20),c_money MONEY)
    DECLARE @fen_b TABLE ([year] INT ,[month] INT ,WORK NVARCHAR(20),d_money MONEY)
    INSERT INTO @zong ( YEAR ,  month , WORK , a_money , b_money , c_money , d_money )
     SELECT   2012 ,   8 ,   N'一部' ,   100 ,  200 ,   0 ,  0 UNION ALL
     SELECT   2012 ,   8 ,   N'二部' ,   50  ,  150 ,   0 ,  0
    INSERT INTO @fen_a ( year, month, WORK, c_money )
     SELECT   2012,  8,  N'一部',  200 UNION ALL
     SELECT   2012,  8,  N'三部',  100  
    INSERT INTO @fen_b ( year, month, WORK, d_money )
     SELECT 2012,  8,  N'二部',  300   UNION ALL
     SELECT 2012,  8,  N'三部',  90   UNION ALL
     SELECT 2012,  8,  N'四部',  120 
     
     DECLARE @x TABLE( [YEAR] INT ,[month] INT ,WORK NVARCHAR(20),a_money MONEY,b_money MONEY,c_money MONEY,d_money MONEY)
     
     INSERT INTO @x ( YEAR , month , WORK , a_money , b_money , c_money , d_money )
    SELECT YEAR , month , WORK , SUM(a_money) , SUM(b_money) , SUM(c_money) , SUM(d_money) FROM (
        SELECT YEAR , month , WORK , a_money , b_money , c_money , d_money  FROM @zong
        union
    SELECT YEAR , month , WORK , 0 , 0 , c_money , 0  FROM @fen_a
    union
    SELECT YEAR , month , WORK , 0 , 0 , 0       , d_money FROM @fen_b
    ) a GROUP BY a.YEAR,a.month,a.WORK


    SELECT * FROM @x; ----------------------------------------------end