读书人

救急 表操作有关问题

发布时间: 2012-02-06 15:52:44 作者: rapoo

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

[解决办法]
create table T(a int)
insert T select 10
union all select 20
union all select 8
union all select 6

select ID=identity(int, 1, 1),* into #T from T

select 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)

读书人网 >SQL Server

热点推荐