我想在oracle中实现这样一个查询,并返回结果集呢. 用视图还是存储过程呢.找出日期是今天的数据,如果没有今天的数据就要返回
和今天离的最近的一天的数据,怎么写呢。
我这个查询可以实现的,但是在在今天和距离今天最近的一天都有数据的时候都查询出来了.
select *  from classinfo where curr_date =sysdate or curr_date = (select max(curr_date)  from Rates  where curr_date < sysdate);

解决方案 »

  1.   

    这个(跟你描述的有些区别,看看你的数据是否可用):select * from classinfo where curr_date = least(sysdate, (select max(curr_date) from classinfo where curr_date < sysdate));
    不行的话,写个存储过程来判断。
      

  2.   

    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;
      

  3.   

      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;我猜大概可能这样的.
      

  4.   

    你不应该通过捕获异常来实现业务逻辑的处理。
    你在java中捕获异常后除了打异常日志,抛出异常外,还进行过业务逻辑的处理???
      

  5.   


    select * from classinfo c1 where not exists (select 1 from classinfo where curr_date>c.curr_date);
    不知楼主的rates是什么表,有什么关系
      

  6.   

    支持
    不过这里需要curr_date不包含hhmmss