两张表
User: UID, U_Status, U_Point
Goods: UID, G_PointGoods表中可能有多个相同的UID, 把相同UID的G_Point合计一下,追加到User表中,且U_Status=1
也就是说: 
            User.U_Point = User.U_Point + Sum(Goods.G_Point)
            User.UID = Goods.UID
           User.U_Status = 1这个存储过程应该怎么写?

解决方案 »

  1.   

    update a set U_Point = U_Point + b.G_Point 
    from User a 
    join (select Uid,sum(G_Point) G_Point from  Goods group by UID )b on a.UID = b.Uid 
    where U_Status   =   1 
      

  2.   


    update a
    set U_Point=a.U_Point+b.con
    from [user] a join (select count(1) as con,UID from Goods group by UID) b on a.UID=b.UID
      

  3.   

    update   a 
    set   U_Point=a.U_Point+b.con 
    from   
    [user]   a   
    join   
    (select   count(1)   as   con,UID   from   Goods   group   by   UID)   b   on   a.UID=b.UID
    and a.U_Status   =   1 --------
    update [user]set U_Point=U_Point+isnull((select count(1) from Goods where UID=[User].UID),0)
    where

    U_Status   =   1 
      

  4.   

    update user
    set  a.U_Point   =   a.U_Point   +  b.G_Point
    from user a,
    (select UID,sum(G_Point) as G_Point from goods group by UID) b
    where  a.UID   =   b.UID 
    and a.U_Status   =   1 
      

  5.   

    楼主自己都快写出来了create procedure pUTotalUser
    as begin 
    update a set U_Point = U_Point + b.G_Point 
    from [User] a 
    join (select Uid,sum(G_Point) G_Point from  Goods group by UID )b on a.UID = b.Uid 
    where U_Status   =   1 
    end 
      

  6.   

    --上面搞成记录了,改一下:
    update   a 
    set   U_Point=a.U_Point+b.con 
    from   
    [user]   a   
    join   
    (select   sum(G_Point)  as   con,UID   from   Goods   group   by   UID)   b   on   a.UID=b.UID
    and a.U_Status   =   1 --------
    update [user]set U_Point=U_Point+isnull((select sum(G_Point) from Goods where UID=[User].UID),0)
    where

    U_Status   =   1 
      

  7.   


    create proc pr_sumPoint
    as
    begin
    update user
    set  a.U_Point   =   a.U_Point   +  b.G_Point
    from user a,
    (select UID,sum(G_Point) as G_Point from goods group by UID) b
    where  a.UID   =   b.UID 
    and a.U_Status   =   1 
    end
      

  8.   


    -- 准备
    create table users(
    uid varchar(20),
    u_status varchar(20),
    u_point int
    )create table goods(
    uid varchar(20),
    g_point int
    )
    insert into users 
    select 'a',null,0 union all
    select 'b',null,0 union all
    select 'c',null,0 insert into Goods 
    select 'a',1 union all
    select 'a',2 union all
    select 'b',20 -- 更改
    update users 
    set u_status = (select 1 from goods where goods.uid = users.uid group by goods.uid),
        u_point = (select sum(g_point) from goods where goods.uid = users.uid group by goods.uid)
    uid                  u_status             u_point     
    -------------------- -------------------- ----------- 
    a                    NULL                 0
    b                    NULL                 0
    c                    NULL                 0
    uid                  g_point     
    -------------------- ----------- 
    a                    1
    a                    2
    b                    20
    结果:
    uid                  u_status             u_point     
    -------------------- -------------------- ----------- 
    a                    1                    3
    b                    1                    20
    c                    NULL                 NULL(所影响的行数为 3 行)uid                  g_point     
    -------------------- ----------- 
    a                    1
    a                    2
    b                    20(所影响的行数为 3 行)
      

  9.   

    且U_Status=1 是最后要把status 更改成1,还是只有status = 1 的才累计?
    我晕,呵呵
      

  10.   

    update a set U_Point = U_Point + b.G_Point 
    from User a 
        join (select Uid,sum(G_Point) G_Point from  Goods group by UID )b on a.UID = b.Uid 
    where U_Status   =   1