create TABLE t(a,int)
insert t select 10
select 20
select 8
select 6
......
让下一条记录减去上一条记录
把结果存在 另外一列里
怎么做啊 
谢谢 各位了

解决方案 »

  1.   

    我写错了 代码是这样的
    create table T(a int)
    insert T select 10
    union all select 20
    union all select 8
    union all select 6
    ..............
      

  2.   

    create table T(a int)
    insert T select 10
    union all select 20
    union all select 8
    union all select 6select ID=identity(int, 1, 1),* into #T from Tselect a, col2=isnull((select a from #T where ID=tmp.ID+1), 0)-a
    from #T as tmp--result
    a           col2        
    ----------- ----------- 
    10          10
    20          -12
    8           -2
    6           -6(4 row(s) affected)
      

  3.   

    大哥 #t tep 是什么东西啊
      

  4.   

    那是临时表的别名
    #t是临时表 tep是它的别名
      

  5.   

    create table T(a int)
    insert T select 10
    union all select 20
    union all select 8
    union all select 6select ID=identity(int, 1, 1),* into #T from Tselect a, col2=a-isnull((select a from #T where ID=tmp.ID-1), 0)
    from #T as tmp--result
    a           col2        
    ----------- ----------- 
    10          10
    20          10
    8           -12
    6           -2(4 row(s) affected)
      

  6.   

    isnull((select a from #T where ID=tmp.ID-1), 0)
    是把0替换成? 
    他也没有值啊 
      

  7.   

    isnull((select a from #T where ID=tmp.ID-1), 0)--
    把NULL替换成0
      

  8.   

    哦 对 
    那isnull((select a from #T where ID=tmp.ID-1), 0)他的结果就全是0 那有什么意义啊