已知: 
table1 结构如下: 
商品号 数量 旧运搬器号 新运搬器号 
10    5    A5       A6 table2结构如下: 
发票号 商品号 数量 运搬器号  
1     10    2    A5 
2     10    4    A5 
--------------------- 
注:(一个运搬器里可以装多个不同发票号的同一商品) 
--------------------- 操作: 
把商品从运搬器A5拿到运搬器A6中,前提只知道只知道table1中的信息,不知道商品具体对应的发票号 
根据表table1来更新table2 
得到的结果如下: 
table2: 
发票号 商品号 数量 运搬器号  
1     10    0    A5 
2     10    1    A5 
1     10    2    A6 
2     10    3    A6 
或 
table2: 
发票号 商品号 数量 运搬器号  
1     10    1    A5 
2     10    0    A5 
1     10    1    A6 
2     10    4    A6 
table2还有一个状态位字段,如果商品的数量为0时把它的值置为2废除状态
例如:
table2: 
发票号 商品号 数量 运搬器号  状态位
1     10    0    A5    2
2     10    1    A5    0
1     10    2    A6    0
2     10    3    A6    0
或  
table2: 
发票号 商品号 数量 运搬器号  状态位
1     10    1    A5    0
2     10    0    A5    2
1     10    1    A6    0
2     10    4    A6    0

解决方案 »

  1.   

    看来我猜谜语的技能还是可以滴:表B是原始表:
    现在来了一个任务,任务在表A中。
    不管采用什么方式完成任务。
    最后可以得到新的表B,如果新的表B中数量有为零的,就将状态置为2。说了这么多,相信大家还是云里雾里。
    简单的说法就是:按照A的要求,将表B中对应商品从旧值变为新值
    10    5    A5       A6  就是说表B中的商品号为10的并且放在A5中的商品,有5件必须放到A6中。
    如果有A5,A6就直接修改,如果没有就新增。
    这里存在一加一减 A5的要减,A6(可能是新增出来的)的要加,数量一致。
      

  2.   

    已知:   
    table1 结构如下:   
    商品号 数量 旧运搬器号 新运搬器号   
    10    5    A5       A6   table2结构如下:   
    发票号 商品号 数量 运搬器号    
    1     10    2    A5   
    2     10    4    A5   
    ---------------------   
    注:(一个运搬器里可以装多个不同发票号的同一商品)   
    ---------------------   操作:   
    把商品从运搬器A5拿到运搬器A6中,前提只知道只知道table1中的信息,不知道商品具体对应的发票号   
    根据表table1来更新table2   
    得到的结果如下:   
    table2:   
    发票号 商品号 数量 运搬器号    
    1     10    0    A5   
    2     10    1    A5   
    1     10    2    A6   
    2     10    3    A6   
    或   
    table2:   
    发票号 商品号 数量 运搬器号    
    1     10    1    A5   
    2     10    0    A5   
    1     10    1    A6   
    2     10    4    A6   
    table2还有一个状态位字段,如果商品的数量为0时把它的值置为2废除状态  
    例如:  
    table2:   
    发票号 商品号 数量 运搬器号  状态位  
    1     10    0    A5    2  
    2     10    1    A5    0  
    1     10    2    A6    0  
    2     10    3    A6    0  
    或    
    table2:   
    发票号 商品号 数量 运搬器号  状态位  
    1     10    1    A5    0  
    2     10    0    A5    2  
    1     10    1    A6    0  
    2     10    4    A6    0 
    ---------------------------------------------------------------- 
    表B是原始表:  
    现在来了一个任务,任务在表A中。  
    不管采用什么方式完成任务。  
    最后可以得到新的表B,如果新的表B中数量有为零的,就将状态置为2。  说了这么多,相信大家还是云里雾里。  
    简单的说法就是:按照A的要求,将表B中对应商品从旧值变为新值  
    10    5    A5       A6    就是说表B中的商品号为10的并且放在A5中的商品,有5件必须放到A6中。  
    如果有A5,A6就直接修改,如果没有就新增。  
    这里存在一加一减 A5的要减,A6(可能是新增出来的)的要加,数量一致。 
      

  3.   

    create table ta(a varchar(10),b varchar(10),c varchar(10),m int)
    insert ta select 'J1','A1','A2',12
    union select'J2','A1','A2',10
    create table tb(a varchar(10),b varchar(10),d varchar(10),n int,t int)
    insert tb select'J1','A1','B1',5,1
    union all select'J1','A1','B2',12,1
    union all select'J1','A1','B2',10,1
    union all select'J2','A1','B1',5,1
    union all select'J2','A1','B2',7,1
    union all select'J2','A1','B1',5,1
    union all select'J1','A2','B1',3,1
    union all select'J1','A2','B2',5,1
    union all select'J3','A2','B1',3,1
    union all select'J3','A2','B2',5,1--看看有哪些要减的
    select identity(int,1,1) id,*,0 zs into # from tb a where exists(select 1 from ta where a.a = a and a.b=b)
    update z set zs = b.zs from # z,(
    select id,isnull((select sum(n) from # where a.a=a and a.b = b and  a.id>id),0) as zs from # a 
    ) b where z.id = b.id
    update a set zs =  zs - b.m from # a,ta b where a.a = b.a and a.b = b.b
    delete # where zs >=0
    update # set zs = 0 - n where n+zs <0
    --更新要减的
    update tb set n = a.n+b.zs from tb a,# b
       where a.a = b.a and a.b = b.b and a.d = b.d and a.n = b.n and a.t = b.t
    --看看那些要加的
    update # set b = b.c from # a,ta b where a.a = b.a and a.b = b.b
    --补齐没有的
    insert tb select a,b,d,0,1 from # a where not exists(select 1 from tb where a.a = a and a.b = b and a.d = d)
    --更新要加的
    update tb set n = a.n-b.zs from tb a,# b where a.a = b.a and a.b = b.b and a.d = b.d 
    --更新状态
    update tb set t = 0 where n = 0
    select * from tb
    /*
    a          b          d          n           t           
    ---------- ---------- ---------- ----------- ----------- 
    J1         A1         B1         0           0
    J1         A1         B2         5           1
    J1         A1         B2         10          1
    J2         A1         B1         0           0
    J2         A1         B2         2           1
    J2         A1         B1         0           0
    J1         A2         B1         8           1
    J1         A2         B2         12          1
    J3         A2         B1         3           1
    J3         A2         B2         5           1
    J2         A2         B1         5           1
    J2         A2         B2         5           1
    */
    drop table ta,tb,#