welyngj(不做老实人) :"最近"的量化標準就是离今日最近的日期,至於說結果有"几條記錄" 就是數據庫中有几條紀錄就查出几紀錄,因為這個是個模糊查詢,好像是要用到 like %P479% 的.
--格式化日期格式 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));
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);
to:sanoul(垃圾),首先謝謝你的幫助,你的兩個方法我都試過了,好像都不行的,因為你的 內層查詢select max(buy_date) from tablename where to_date(buy_date)<>to_date(sysdate)); 只能查詢系統中所有料號的最大日期,而且只有一個值,而我要的是根據料號進行 分組的,thank you all the same!
或者建立一個簡單視圖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
TO: bzszp(www.bzszp.533.net) AND sanoul(垃圾) ,謝謝兩位的幫忙,現在按照bzszp(www.bzszp.533.net)的提示,問題已經得到解決,下班後結賬!
就是數據庫中有几條紀錄就查出几紀錄,因為這個是個模糊查詢,好像是要用到 like %P479% 的.
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));
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);
內層查詢select max(buy_date) from tablename where to_date(buy_date)<>to_date(sysdate));
只能查詢系統中所有料號的最大日期,而且只有一個值,而我要的是根據料號進行
分組的,thank you all the same!
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