看这样行不行:
SELECT sum(money) FROM tab WHERE flag=1 and MONTHS_BETWEEN(curdate, sysdate)<=intervial group by name;
不过,我没试过months_between的右值是表中字段的情形。

解决方案 »

  1.   

    不对啊,我不需要和sysdate来进行判断
      

  2.   

    如果非要用一条语句的话会比较慢,不过数据量小的话可以考虑:
    select name,sum(money) from tab a where curdate between (select cur_date from tab
    where flag =1 and name = a.name) and (select add_months(cur_date,intervial) from tab
    where flag =1 and name = a.name) group by name还是建议你写程序或存储过程,函数之类的
      

  3.   

    doisadfsadf(怀念黑豹一) :
    请问如何来写存储过程呢?请赐教!
      

  4.   

    脚本就可以了
    先建表my_tab:
    create table my_tab (name varchar2(50),money number(10));然后执行以下脚本declare 
    m_interval number(6);
    m_name varchar2(50);
    m_curdate date;
    m_money number(10);
    type curtype is ref cursor;
    m_cur curtype;
    sql varchar2(200);begin
    sql := 'select name,interval,curdate from tab where flag =1';
    open m_cur for sql;
    loop
    fetch m_cur into m_name,m_interval,m_curdate;
    exit when m_cur%notfound;select nvl(sum(money),0) into m_money from tab where name = m_name and curdate between m_curdate and add_months(m_curdate,m_interval);insert into my_tab values(m_name,m_money);end loop;
    close m_cur;commit;end;
    /
      

  5.   

    select name,nvl(money,0)+nvl((
    select sum(money) from tab where name=t1.name and curdate>t1.curdate and curdate<=add_months(t1.curdate,t1.intervial)),0)
    from tab t1
    where flag=1;