表ck
 
  id      ye1      ye2           
   1        2      3
   2        3     4
   3        4      5
想得到
   
  id      ye1      ye2    ye        
   1        7      2      5(7-2)
   2        3     4       4(5+3-4)
   3        4      5      3(4+4-5)如何得到这个ye     

解决方案 »

  1.   

    先别说ye,前面那个ye1 7,ye2 2 从何而来?下面两行数怎么又不变了?
      

  2.   

    猜一个,我猜楼主是写错数据了,第一行应该是1 7 2---测试数据---
    if object_id('[ck]') is not null drop table [ck]
    go
    create table [ck]([id] int,[ye1] int,[ye2] int)
    insert [ck]
    select 1,7,2 union all
    select 2,3,4 union all
    select 3,4,5
     
    ---查询---
    select 
      *,
      ye=(select sum(ye1)-sum(ye2) from ck where id<=t.id)
    from ck t---结果---
    id          ye1         ye2         ye          
    ----------- ----------- ----------- ----------- 
    1           7           2           5
    2           3           4           4
    3           4           5           3(所影响的行数为 3 行)
      

  3.   

    树哥,好强哦!不过他的数据是
    id      ye1      ye2          
      1        2      3 
      2        3    4 
      3        4      5 
    这样的好理解一些吧
      

  4.   

    ---测试数据---
    if object_id('[ck]') is not null drop table [ck]
    go
    create table [ck]([id] int,[ye1] int,[ye2] int)
    insert [ck]
    select 1,7,2 union all
    select 2,3,4 union all
    select 3,4,5
     
    ---查询---
    select 
      *,
      ye=(select sum(ye1)-sum(ye2) from ck where id<=t.id)
    from ck t---结果---
    id          ye1         ye2         ye          
    ----------- ----------- ----------- ----------- 
    1           7           2           5
    2           3           4           4
    3           4           5           3(所影响的行数为 3 行)