insert into TotalTable1 (a,b,c,d)
    select distinct a,b,c,0 form Total1 as ta 
        where not exists (select * from TotalTable1 where ta.a=tb.a and ta.b=tb.b and ta.c=tb.c)update TotalTable1 
    set d = ta.d
    from TotalTable1, 
         (select a,b,c,sum(d) as d from Table1 group by a,b,c) as ta
    where  ta.a=TotalTable1.a and ta.b=TotalTable1.b and ta.c=TotalTable1.c

解决方案 »

  1.   


    select a, b, c, sum(d) d
    into #temp
    from  table1
    group by a, b, cunion allselect a, b, c, sum(e)
    from totaltable1delete from totaltable1insert into totaltable1
    select * from #temp
      

  2.   

    select a, b, c, sum(d) d
    into #temp
    from  table1
    group by a, b, cunion allselect a, b, c, e
    from totaltable1delete from totaltable1insert into totaltable1
    select * from #temp
      

  3.   

    UPDATE TotalTable1 x
       SET E = (SELECT SUM(D)
                  FROM Table1 y
                 WHERE y.a = x.a
                   AND y.b = x.b
                   AND y.c = x.c)
     WHERE E != (SELECT SUM(D)
                   FROM Table1 z
                  WHERE z.a = x.a
                    AND z.b = x.b
                    AND z.c = x.c);INSERT INT TotalTable1
    SELECT a, b, c, sum(d)
      FROM Table1 x
     WHERE NOT EXISTS (SELECT 'x'
                         FROM TotalTable1 y
                        WHERE y.a = x.a
                          AND y.b = x.b
                          AND y.c = x.c)
     GROUP BY a, b,c
      

  4.   

    update TotalTable1 set
       E=E+(select sum(D) from Table1 a
    where exists(select a from TotalTable1 b where a.a=b.a and a.b=b.b and a.c=b.c) group by a.a,a.b,a.c)insert into TotalTable1
      select A,B,C,sum(D) from Table1 a 
      where not exists(select a from TotalTable1 b where a.a=b.a and a.b=b.b and a.c=b.c) group by a.a,a.b,a.c ;