如下:    a          b  c
1    9712 6123 2032
2    9735 6133 2034
3    9758 6143 2033
4    9780 6153 2034
5    9803 6163 2034
要分别得知a5-a1,b5-b1,c5-c1的值,应该怎样操作,谢谢了。

解决方案 »

  1.   

    select T1.a-T2.a,T1.b-T2.b,T1.c-T2.c
    from
    (select a,b,c
    from tb
    where idx=5) T1
    (select a,b,c
    from tb
    where idx=1)T2
      

  2.   

    select 
    isnull(b.a,0)-a.a as [a5-a1],
    isnull(b.b,0)-a.b as [b5-b1],
    isnull(b.c,0)-a.c as [c5-c1],
    from 
    table1 a
    left join 
    table1 b on b.id=5 
    where a.id=1
      

  3.   

    select max(a)-min(a),max(b)-min(b),max(c)-min(c)
    from tablename
      

  4.   


    select 
    b.a-a.a as [a5-a1], b.b-a.b as [b5-b1], b.c-a.c as [c5-c1]
    from 
    (select top 1 * from table1 order by id) a 
    cross join 
    (select top 1 * from table1 order by id desc) b 
      

  5.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[a] int,[b] int,[c] int)
    insert [tb]
    select 1,9712,6123,2032 union all
    select 2,9735,6133,2034 union all
    select 3,9758,6143,2033 union all
    select 4,9780,6153,2034 union all
    select 5,9803,6163,2034---查询---
    select 
      b.a-a.a as [amax-amin],
      b.b-a.b [bmax-bmin],
      b.c-a.c [cmax-cmin] 
    from
    (select * from tb where id in(select min(id) from tb)) a,
    (select * from tb where id in(select max(id) from tb)) b---结果---
    amax-amin   bmax-bmin   cmax-cmin   
    ----------- ----------- ----------- 
    91          40          2(所影响的行数为 1 行)