是相临两行吗??如果是你可以有游标的declare @temp int ,@temp1 ,int @result int
declare cur scroll cursor for select num from table
open cur
fetch first form cur into @temp
while (@@fetch_status=0)
begin
set @temp1 =temp
fetch next from cur into temp
select @resulrt =temp1-@termp//得到你的结果
endclose cur
deallocate cur
declare cur scroll cursor for select num from table
open cur
fetch first form cur into @temp
while (@@fetch_status=0)
begin
set @temp1 =temp
fetch next from cur into temp
select @resulrt =temp1-@termp//得到你的结果
endclose cur
deallocate cur
我正处理一个隔行数据处理那就用:where b.flag=a.flag+2 免得用游标写
半死---(顿悟!)
?where b.flag=a.flag+2 你能保证这个这样做是隔行
where b.flag=a.flag+1 是临行吗??
on a.flag = b.flag - 1
num-(select num from tablename b where b.flag=a.flag+1)
减号后面括号里面的必须保证返回值唯一才可以的:)大家多交流,讨论
declare @temp1 int
declare @result int
declare cur scroll cursor for select num from urtable
open cur
fetch first from cur into @temp
while (@@fetch_status=0)
begin
set @temp1 =@temp
fetch next from cur into @temp
select @result =@temp1-@temp --/得到你的结果
select @result
endclose cur
deallocate cur 修改了一下,可以用的
可以实现数据相减,现又遇到一问题
表一
cus_id flag num
140002 1 234
140002 2 233
140005 1 245
140009 2 200
140010 1 300
140010 2 100
需得结果如下: x_num 为表 1 相同的 CUS_ID FLAG为1和为2 的数据相减,如flag 为 1 或 2 的情况有一项不存在,则默认该项为空
cus_id x_num
140002 1
140005 245
140009 -200
140010 200
现在能的出当flag 1 和 2 都存在的情况,但当同一个cus_id 只有一个flag 时,x_num就不能得出正确数据,求教,,,多谢!!!!!
现在能得到的结果
cus_id x_num
140002 1
140005 NULL -----结果应为245
140009 NULL -----结果应为-200
140010 200