有这样一个递归的规律:
如:
 月份   no
200610  7
200611  8
200612  9
200701  10
200702  11
200703  12
200704  13
200705  1
200706  2
200707  3
200708  4
200709  5
200710  6
以此类推,请问有没有什么算法或什么办法能在当月得到后面no的值?

解决方案 »

  1.   

    select pmonth, decode(sign(num-13),0,1,num +1)  as next_num from (
    select '200610' as pmonth, 7  as num from dual union all
    select '200611' as pmonth, 8  as num from dual union all
    select '200612' as pmonth, 9  as num from dual union all
    select '200701' as pmonth, 10 as num from dual union all
    select '200702' as pmonth, 11 as num from dual union all
    select '200703' as pmonth, 12 as num from dual union all
    select '200704' as pmonth, 13 as num from dual union all
    select '200705' as pmonth, 1  as num from dual union all
    select '200706' as pmonth, 2  as num from dual union all
    select '200707' as pmonth, 3  as num from dual union all
    select '200708' as pmonth, 4  as num from dual union all
    select '200709' as pmonth, 5  as num from dual union all
    select '200710' as pmonth, 6  as num from dual 
    ) where pmonth = '200704'
      

  2.   

    那要是 pmonth 是 200804 呢? 这样是不是太长了?
      

  3.   

    select mod(months_between(to_date(&yearmonth,'yyyymm'), to_date('200610','yyyymm'))+7,13)+1 from dual
      

  4.   

    谢谢你的启发,你的语句有个小问题。就是当参数是 200704 等月份数相差13的时候你取mod那么取出来为0,而这时值应该是13我修改了一下。select decode(mod(months_between(to_date(&yearmonth,'yyyymm'), to_date('200610','yyyymm'))+7,13),0,13,mod(months_between(to_date(&yearmonth,'yyyymm'), to_date('200610','yyyymm'))+7,13)) from dual;
      

  5.   

    不知道楼主怎么执行的,
    SQL> select mod(months_between(to_date('200704','yyyymm'), to_date('200610','yyyymm'))+7,13)+1 from dual
      2  /MOD(MONTHS_BETWEEN(TO_DATE('20
    ------------------------------
                                 1执行出来的结果正是楼主需要200704月份的下一个月200705对应的值1,而不是楼主说的什么0
    这个sql执行的结果只可能是1-13,不可能出现0值
      

  6.   

    如果需要的是当月的话的值
    SQL>  select mod(months_between(to_date('200704','yyyymm'), to_date('200610','yyyymm'))+7-1,13)+1 from dual
      2  /MOD(MONTHS_BETWEEN(TO_DATE('20
    ------------------------------
                                13
      

  7.   

    11月16日的语句:
    select mod(months_between(to_date(&yearmonth,'yyyymm'), to_date('200610','yyyymm'))+7,13)+1 from dual11月17日的语句:
    select mod(months_between(to_date(&yearmonth,'yyyymm'), to_date('200610','yyyymm'))+7-1,13)+1 from dual两次语句的"+7"后面不一样.不过谢谢你.搞定了.马上给分