表  tableid    in   out  
1     10   20
2     10   30
3     10   40==============================怎么样查询出out-int值最大的那条记录?

解决方案 »

  1.   

    select top 1 *
    from table
    order by out desc
      

  2.   

    select
      *
    from 
      [table] t
    where 
      not exists(select * from [table] where ([out]-[in])>(t.[out]-t.[in])
      

  3.   

    if object_id('[table]') is not null drop table [table]
    go
    create table [table]([id] int,[in] int,[out] int)
    insert [table]
    select 1,10,20 union all
    select 2,10,30 union all
    select 3,10,40select
      *
    from 
      [table] t
    where 
      not exists(select * from [table] where ([out]-[in])>(t.[out]-t.[in]))--测试结果:
    /*
    id          in          out         
    ----------- ----------- ----------- 
    3           10          40(所影响的行数为 1 行)
    */
      

  4.   

    select top 1 *
    from table
    order by out-in desc
      

  5.   

    select top 1 *
    from table
    order by [out]-[in] desc
      

  6.   


    select top 1 * 
    from table 
    order by [out] - [IN] desc
      

  7.   

    select top 1 * 
    from table 
    order by [out] - [IN] desc 
      

  8.   

    select top 1 *
    from tb
    order by out-in desc