这样的查询如何写呢

truelove7283159 2010-05-08 12:06:40
我想在oracle中实现这样一个查询,并返回结果集呢. 用视图还是存储过程呢.

找出日期是今天的数据,如果没有今天的数据就要返回
和今天离的最近的一天的数据,怎么写呢。
我这个查询可以实现的,但是在在今天和距离今天最近的一天都有数据的时候都查询出来了.


select * from classinfo where curr_date =sysdate or curr_date = (select max(curr_date) from Rates where curr_date < sysdate);
...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqtjffcu 2010-05-08
  • 打赏
  • 举报
回复

select * from classinfo c1 where not exists (select 1 from classinfo where curr_date>c.curr_date);

不知楼主的rates是什么表,有什么关系
dengjie12345 2010-05-08
  • 打赏
  • 举报
回复
应该可以的,把
duqiangcise 2010-05-08
  • 打赏
  • 举报
回复
你不应该通过捕获异常来实现业务逻辑的处理。
你在java中捕获异常后除了打异常日志,抛出异常外,还进行过业务逻辑的处理???
truelove7283159 2010-05-08
  • 打赏
  • 举报
回复
select * from Rates where curr_date = to_date('2010-05-09','YYYY-MM-DD');
exception
when NO_DATA_FOUND then
select * from Rates where curr_date = (select max(curr_date) from Rates where curr_date < to_date('2010-05-09','YYYY-MM-DD') );
end;

我猜大概可能这样的.
duqiangcise 2010-05-08
  • 打赏
  • 举报
回复
create or replace procedure test_proc(p_cur out sys_refcursor)
as
l_count number:=0;
begin
select count(*) into l_count from classinfo where curr_date >=to_date(sysdate,'yyyy-mm-dd') and curr_date <=to_date(sysdate+1,'yyyy-mm-dd');
if l_count =0 then
open p_cur for select * from classinfo where curr_date =(select max(curr_date) from classinfo);
else
open p_cur for select * from classinfo where curr_date >=to_date(sysdate,'yyyy-mm-dd') and curr_date <=to_date(sysdate+1,'yyyy-mm-dd');
end;
end;
codearts 2010-05-08
  • 打赏
  • 举报
回复
这个(跟你描述的有些区别,看看你的数据是否可用):

select * from classinfo where curr_date = least(sysdate, (select max(curr_date) from classinfo where curr_date < sysdate));


不行的话,写个存储过程来判断。


小灰狼W 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ssqtjffcu1 的回复:]
SQL code

select * from classinfo c1 where not exists (select 1 from classinfo where curr_date>c.curr_date);

不知楼主的rates是什么表,有什么关系
[/Quote]
支持
不过这里需要curr_date不包含hhmmss

17,380

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧