select top 1 * from 表a where stallid='A001' order by abs(datediff(second,paydate,'2008-01-05'))

解决方案 »

  1.   

    --相差天数最近的记录,那用day就能满足了:
    select top 1 * from 表a where stallid='A001' order by abs(datediff(day,paydate,'2008-01-05'))
      

  2.   

    select top 1 *
    from ta
    where stallid= 'A001' and abs(datediff(mi,paydate,'2008-01-05')) asc
      

  3.   

    表结构更正一下,应收日期相同日,费用可能有多不同,比如租金,管理费
    stallid(房间),infoid(合同号),costname(费用名称),paydate(应收日期),payvalue(应收金额) 
      

  4.   


    select top 1 *
    from tableName
    where stallid= 'A001' 
    order by abs(datediff(day,paydate,'2008-01-05'))
      

  5.   


    select  *
    from ta
    where stallid= 'A001' and abs(datediff(mi,paydate,'2008-01-05')) asc
      

  6.   


    select * from 表a t where stallid='A001' and paydate=(select top 1 paydate from 表a where stallid=t.stallid order by abs(datediff(day,paydate,'2008-01-05')))
      

  7.   

    select * from 表a t where stallid='A001' and paydate=(select top 1 paydate from 表a where stallid=t.stallid and infoid=t.infoid order by abs(datediff(day,paydate,'2008-01-05')))
      

  8.   

    stallid(房间),infoid(合同号),costname(费用名称),paydate(应收日期),payvalue(应收金额) 
    a01              2008a01        租金             2008-01-01       3000
    a01              2008a01        物业管理费       2008-01-01       100
    a01              2008a01        租金             2008-02-01       3000
    a01              2008a01        物业管理费       2008-02-01       100
    a01              2008a01        租金             2008-03-01       3000
    a01              2008a01        物业管理费       2008-03-01       100
    a02              2008a02        租金             2008-01-01       3000
    a02              2008a02        物业管理费       2008-01-01       200
    a02              2008a02        租金             2008-02-01       3000
    a02              2008a02        物业管理费       2008-02-01       200
    a02              2008a02        租金             2008-03-01       3000
    a02              2008a02        物业管理费       2008-03-01       200比如输入:A01,日期2008-01-05
    要求得到:
    a01              2008a01        租金             2008-01-01       3000
    a01              2008a01        物业管理费       2008-01-01       100
    比如输入:A01,日期2008-01-20
    a01              2008a01        租金             2008-02-01       3000
    a01              2008a01        物业管理费       2008-02-01       100