alter session set nls_date_format='YYYY-MM-DD';select * from tablename
 where id like 'P479%'
   and buy_date=(select max(buy_date) from tablename);

解决方案 »

  1.   

    welyngj(不做老实人) :"最近"的量化標準就是离今日最近的日期,至於說結果有"几條記錄"
    就是數據庫中有几條紀錄就查出几紀錄,因為這個是個模糊查詢,好像是要用到 like %P479% 的.
      

  2.   

    --格式化日期格式
    alter session set nls_date_format='YYYY-MM-DD';--選擇最近的日期
    select max(buy_date) from tablename
     where to_date(buy_date)<>to_date(sysdate);--選擇妳需要的結果
    select * from tablename
     where id like 'P479%'
       and buy_date=(select max(buy_date) 
                       from tablename 
                      where to_date(buy_date)<>to_date(sysdate));
      

  3.   

    select  *  from  tablename  
     where  id  like  'P479%'  
         and exists(select 1 from (select  id,max(buy_date) maxdt  from  tablename
    group by id) t where t.id=tablename.id and t.maxdt=tablename.buy_date);
      

  4.   

    to:sanoul(垃圾),首先謝謝你的幫助,你的兩個方法我都試過了,好像都不行的,因為你的
    內層查詢select max(buy_date) from tablename where to_date(buy_date)<>to_date(sysdate));
    只能查詢系統中所有料號的最大日期,而且只有一個值,而我要的是根據料號進行
    分組的,thank you all the same!
      

  5.   

    只要在bzszp的语句中加一句where to_date(buy_date)<>to_date(sysdate)就可以了,位置在exist内部的from子查询
      

  6.   

    或者建立一個簡單視圖viewcreate view v_tablename as
      select id,max(buy_date) recent_date 
        from tablename
       where where to_date(buy_date)<>to_date(sysdate)
       group by id;而后
    select * from tablename t,v_tablename v
     where t.id like 'P479%'
       and t.id=v.id
       and t.buy_date=v.recent_date
      

  7.   

    TO: bzszp(www.bzszp.533.net) AND sanoul(垃圾) ,謝謝兩位的幫忙,現在按照bzszp(www.bzszp.533.net)的提示,問題已經得到解決,下班後結賬!