有表table 里有这样条记录
 数量    批次号   时间(系统时间)           名称
. . .  10       pihao   2004-03-08 15:25:40.000    笔
. . .  20       pihao   2004-04-30 08:38:09.000    笔
. . .  35       pihao2   2004-05-30 08:38:09.000   笔
. . .  40       pihao3   2004-06-30 08:38:09.000   笔
. . .  40       pihao3   2004-07-30 08:38:09.000   笔

. . .  30       pihao4  2004-04-30 08:38:33.000    本子在表table中因为时间不一样所以而不同;
先我要按下面得到下面表:现我要从这个表中把“名称=“笔”的所有数量(10+20+35+40+40)减去45,按时间小到的大顺序来减:
最后表table的结果应是:。.数量 批次号  时间(系统时间)           名称
. .0   pihao   2004-03-08 15:25:40.000 笔(10-10)(因为10〈45)
. .0   pihao   2004-04-30 08:38:09.000 笔(20-20)(因为20<(45-10))
. .15  pihao2  2004-05-30 08:38:09.000 笔(35-(45-(10+20)))如果还小继续找;
. .40  pihao3  2004-06-30 08:38:09.000 笔 但不会有(10+20+35+40+40)总量〈45(要减的数)的情况
. 40   pihao3  2004-07-30 08:38:09.000 笔

. 30   pihao4  2004-04-30 08:38:33.000  本子
 
按时间从小到大来确定减的顺序,因为:2004-03-08 15:25:40.000〈2004-04-30 08:38:09.000〈2004-05-30 08:38:09.000<2004-06-30 08:38:09.000 .......
请教sql语句的实现方法,谢谢啊!详细点啊,最好是给上注释谢谢啊!

解决方案 »

  1.   

    --更新
    update a set 数量=case 
    when (select sum(数量) from 表 where 名称=a.名称 and 时间<=a.时间)<=45
    then 0
    else 数量-(45-isnull((select sum(数量) from 表 where 名称=a.名称 and 时间<a.时间),0))
    end
    from 表 a
    where 名称='笔' and isnull((
    select sum(数量) from 表 
    where 名称=a.名称 and 时间<a.时间
    ),0)<45
      

  2.   

    --测试--测试数据
    create table 表(数量 int,批次号 varchar(10),时间 datetime,名称 varchar(10))
    insert 表 select 10,'pihao' ,'2004-03-08 15:25:40.000','笔'
    union all select 20,'pihao' ,'2004-04-30 08:38:09.000','笔'
    union all select 35,'pihao2','2004-05-30 08:38:09.000','笔'
    union all select 40,'pihao3','2004-06-30 08:38:09.000','笔'
    union all select 40,'pihao3','2004-07-30 08:38:09.000','笔'
    union all select 30,'pihao4','2004-04-30 08:38:33.000','本子'
    go--更新
    update a set 数量=case 
    when (select sum(数量) from 表 where 名称=a.名称 and 时间<=a.时间)<=45
    then 0
    else 数量-(45-isnull((select sum(数量) from 表 where 名称=a.名称 and 时间<a.时间),0))
    end
    from 表 a
    where 名称='笔' and isnull((
    select sum(数量) from 表 
    where 名称=a.名称 and 时间<a.时间
    ),0)<45--显示处理结果
    select * from 表
    go--删除测试
    drop table 表/*--测试结果数量          批次号        时间                   名称   
    ----------- ---------- -------------------------- -------
    0           pihao      2004-03-08 15:25:40.000    笔
    0           pihao      2004-04-30 08:38:09.000    笔
    20          pihao2     2004-05-30 08:38:09.000    笔
    40          pihao3     2004-06-30 08:38:09.000    笔
    40          pihao3     2004-07-30 08:38:09.000    笔
    30          pihao4     2004-04-30 08:38:33.000    本子(所影响的行数为 6 行)
    --*/
      

  3.   

    有表table 里有这样条记录
     数量    批次号   时间(系统时间)           名称
    . . .  10       pihao   2004-03-08 15:25:40.000    笔
    . . .  20       pihao   2004-04-30 08:38:09.000    笔
    . . .  35       pihao2   2004-05-30 08:38:09.000   笔
    . . .  40       pihao3   2004-06-30 08:38:09.000   笔
    . . .  40       pihao3   2004-07-30 08:38:09.000   笔

    . . .  30       pihao4  2004-04-30 08:38:33.000    本子在表table中因为时间不一样所以而不同;
    先我要按下面得到下面表:现我要从这个表中把“名称=“笔”的所有数量(10+20+35+40+40)减去45,按时间小到的大顺序来减:
    最后表table的结果应是:。.数量 批次号  时间(系统时间)           名称
    . .0   pihao   2004-03-08 15:25:40.000 笔(10-10)(因为10〈45)
    . .0   pihao   2004-04-30 08:38:09.000 笔(20-20)(因为20<(45-10))
    . .20  pihao2  2004-05-30 08:38:09.000 笔(35-(45-(10+20)))如果还小继续找;
    . .40  pihao3  2004-06-30 08:38:09.000 笔 但不会有(10+20+35+40+40)总量〈45(要减的数)的情况
    . 40   pihao3  2004-07-30 08:38:09.000 笔

    . 30   pihao4  2004-04-30 08:38:33.000  本子
     
    按时间从小到大来确定减的顺序,因为:2004-03-08 15:25:40.000〈2004-04-30 08:38:09.000〈2004-05-30 08:38:09.000<2004-06-30 08:38:09.000 .......
    请教sql语句的实现方法,谢谢啊!详细点啊,最好是给上注释谢谢啊!