原数据结构为:
A   B  C  
A1  1  2  
A2  3  3
A3  2  6
A4  5  8知道A,B两列的值,如何最简单的得到C的值,
C的值除第一值知道外,其它行的值为上一行B+C的值
以此类推得出C的值

解决方案 »

  1.   


    create table tab
    (
    a varchar(2),
    b int,
    c int
    )insert into tab select 'A1',  1,  2  
    insert into tab select 'A2',  3,  3
    insert into tab select 'A3',  2,  6
    insert into tab select 'A4',  5,  8--语句
    declare @a varchar(2)
    set @a = 'A4'
    declare @int int
    select top 1 @int = c from tab select @int = @int + b from tab where a < @a
    select @int@int值既是c的值
      

  2.   

    只需要知道A列的值,即可通过计算上一行B+C,得到相应C值drop table #temp
    create table #temp
    (
    A varchar(2),
    B int,
    C int
    )
    insert into #temp select 'A1',  1,  2  
    insert into #temp select 'A2',  3,  3
    insert into #temp select 'A3',  2,  6
    insert into #temp select 'A4',  5,  8假设知道A='A3'
    select a.B+a.C C
    from #temp a,#temp b 
    where cast(right(a.A,1) as int)=cast(right(b.A,1) as int)-1 and b.A='A3'
    ---
    6