解决方案 »

  1.   

    --先分配存在的
    WITH s1 AS (
        SELECT col_e, col_p, SUM(col_v) v
          FROM t1
      GROUP BY col_e, col_p
    )
    UPDATE t1
       SET t1.col_v = t1.col_v + t2.col_v / s1.v
      FROM t1, s1, t2
     WHERE t1.col_e = s1.col_e AND t1.col_p = s1.col_p
       AND t1.col_e = t2.col_e AND t1.col_p = t2.col_p
    --再添加不存在的
    INSERT INTO t1
    SELECT *
      FROM t2
     WHERE NOT EXISTS (SELECT *
                         FROM t1
                        WHERE t1.col_e = t2.col_e AND t1.col_p = t2.col_p)
      

  2.   

    with T1(col_e,col_p,col_v) as
    (
     select 'e1','p1','v1' union all
     select 'e1','p2','v2' union all
     select 'e2','p3','v3' union all
     select 'e2','p4','v4'
    ),t2(col_e,col_p,col_v) as
    (
     select 'e1','p','v5' union all
     select 'e2','p','v6' union all
     select 'e3','p','v7'
    )select g.col_e,g.col_p,g.col_v+'+'+L.col_v+'*'+g.k  as col_v  from 
    (select *,
    stuff((select '+'+a.col_v from t1 as a where a.col_e=t1.col_e  for xml path(''))+')',1,1,'(') as k
    from T1) as g left join t2 as L on g.col_e=L.col_e 
    union all select * from t2 where not exists(select * from t1 where t1.col_e=t2.col_e)
      

  3.   

    上面写错了,重新修改了一下,万分抱歉with T1(col_e,col_p,col_v) as
    (
     select 'e1','p1','v1' union all
     select 'e1','p2','v2' union all
     select 'e2','p3','v3' union all
     select 'e2','p4','v4'
    ),t2(col_e,col_p,col_v) as
    (
     select 'e1','p','v5' union all
     select 'e2','p','v6' union all
     select 'e3','p','v7'
    )select g.col_e,g.col_p,g.col_v+'+'+L.col_v+'*'+g.col_v+'/'+g.k  as col_v  from (select *,stuff((select '+'+a.col_v from t1 as a where a.col_e=t1.col_e  for xml path(''))+')',1,1,'(') as kfrom T1) as g left join t2 as L on g.col_e=L.col_e union all select * from t2 where not exists(select * from t1 where t1.col_e=t2.col_e)