declare @i int
set @i=100表1:
流水ID   col_1   col_2 
1        30   
2        20
3        40
--------------------
要求结果:
流水ID   col_1   col_2 
1        30      30   
2        20      20
3        60      50 
---------------
说明:把@i按行的先后次序进行分配列col_1,如上,当分配到第三行(即流水ID为3的那一行时)由于@i只剩下50,因此col_2只能为50而不能为60。要求:不用游标,直接用SQL语句实现。
请大家帮帮忙,火线放分!

解决方案 »

  1.   

    表1的流水ID=3的col_1是40?是笔误还是什么的?本来该60的吧
      

  2.   

    create table #1(id int,col1 int,col2 int)
    insert into #1(id,col1) select 1,30
    insert into #1(id,col1) select 2,20
    insert into #1(id,col1) select 3,60
    declare @i int
    set @i=100
    select id,col1,case when ( select sum(col1) from #1 where id<=t.id) >@i then (select sum(col1) from #1 where id<t.id ) else col1 end as '值'from #1 tid col1 值
    1 30 30
    2 20 20
    3 60 50
      

  3.   

    create table #1(id int,col1 int,col2 int)
    insert into #1(id,col1) select 1,30
    insert into #1(id,col1) select 2,20
    insert into #1(id,col1) select 3,60
    declare @i int
    set @i=100
    update tt1 set col2=tt2.col2 from #1 tt1 join (
    select id,col1,case when ( select sum(col1) from #1 where id<=t.id) >@i then (select sum(col1) from #1 where id<t.id ) else col1 end as col2from #1 t)tt2 on tt1.id=tt2.idselect * from #1id col1 col2
    1 30 30
    2 20 20
    3 60 50
      

  4.   

    /******************************************/
    /*回复:代码20080516003 总:00000000016    */
    /*主题:各行分配数值的问题                  */
    /*作者:二等草                             */
    /******************************************/
    set nocount on
    /************例子数据 begin****************/
    declare @t table(id int identity,c1 int,c2 int)
    insert @t(c1) select 30
    insert @t(c1) select 20
    insert @t(c1) select 60/************例子数据 end******************//************代码     begin***************/
    declare @i int 
    set @i=100 
    update @t set c2 = case when @i>=0 then c1 else c1+@i end,@i = @i - c1
    select * from @t
    /************代码     end*****************//************结果     begin***************
    id          c1          c2          
    ----------- ----------- ----------- 
    1           30          30
    2           20          20
    3           60          50
     ************结果     end*****************//************清除*************************/
      

  5.   

    set nocount on
    declare @t table(ID int ,col1 int,col2 int)
    insert into @t select 1,30,null
    union all select 2,20,null
    union all select 3,60,null
    select * from @t
    declare @i int 
    set @i =100
    declare @j int
    declare @k int
    set @k=1
    while @i>0
    begin
    select @j=col1 from @t where ID=@k
    if @i<@j
    update @t set col2=@i where ID=@k
    else
    update @t set col2=@j where ID=@k
    set @i=@i-@j
    set @k=@k+1
    end
    select * from @t
    set nocount offID col1 col2
    1 30
    2 20
    3 60 ID col1 col2
    1 30 30
    2 20 20
    3 60 50
      

  6.   


    select sum(col1) from #1 where id<t.id 
    学习您的帖子时,发现这里不对,请见谅,无恶意
    @i只剩下50,因此col_2只能为50而不能为60   
    并不是对id<t.id 的求和期待修正版