是相临两行吗??如果是你可以有游标的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 

解决方案 »

  1.   

    如果仅仅在select  里得到结果,估计有难度了:)
      

  2.   

    select num-(select num from tablename b where b.flag=a.flag+1) from tablename a
      

  3.   

    where b.flag=a.flag+1 太棒了!
    我正处理一个隔行数据处理那就用:where b.flag=a.flag+2 免得用游标写
    半死---(顿悟!)
      

  4.   

    学习,有道理:)不过有些疑问啊你能保证flag是连续的吗
    ?where b.flag=a.flag+2  你能保证这个这样做是隔行
    where b.flag=a.flag+1   是临行吗??
      

  5.   

    select a.num-b.num from table_name as a left join table_name as b
    on a.flag = b.flag - 1
      

  6.   

    还有如果有重复的行如何处理那?
    num-(select num from tablename b where b.flag=a.flag+1) 
    减号后面括号里面的必须保证返回值唯一才可以的:)大家多交流,讨论
      

  7.   

    declare  @temp  int 
    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 修改了一下,可以用的
      

  8.   

    多谢各位先
    可以实现数据相减,现又遇到一问题
    表一
     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
      

  9.   

    select distinct cust_id,abs(num-case when exists (select num from tablename b where b.num=a.num and b.flag<>a.flag) then (select top 1 num from tablename b where b.num=a.num and b.flag<>a.flag) else 0 end) from tablename a
      

  10.   

    select cus_id,sum(case flag when 1 then num else (-1)*num end) as 结果 from fz group by cus_id