我有一个表
TABLE DT(类型,金额,日期)
现在我想查出某一类型某个日期或是该日期最接近日期的那一批数据,如何查。
比如:
类型 金额 日期
1    200,20110101
1    230,20110201
1    300,20110101
1    202,20110202
1    203,20110202
1    204,20110210当我提交的日期为20110204,可是因为没有该日期的值,便查询出来的为20110202的值,
为:
1    202,20110202
1    203,20110202

解决方案 »

  1.   

    select * from tb a
    where 日期<='20110204'
    and not exists
    (select 1 from tb a where a.类.型=b.类型 and b 日期<='20110204' 
     and b.日期>a.日期)
      

  2.   


    select *
    from tb t
    where 日期 = (select max(日期) from tb where 日期 <= '20110204')
      

  3.   

    select * from tb a
    where 日期<='20110204'
    and not exists
    (select 1 from tb a where a.类型=b.类型 and b.日期<='20110204'
     and b.日期>a.日期)
      

  4.   

    --或
    select *
    from tb t
    where 日期 = (select max(日期) from tb 
                 where 类型=t.类型 and 日期 <= '20110204')
      

  5.   

    select
     *
    from
     tb t
    where
     日期 = (select max(日期) from tb  where 类型=t.类型 and 日期 <= '20110204')