これを試して:
select max(case
when trunc(the_date) between to_date('01-apr-'||the_year, 'dd-mon-yyyy')
and to_date('30-sep-'||the_year, 'dd-mon-yyyy')
and dte < to_date('01-apr-'||the_year, 'dd-mon-yyyy')
then
dte
when trunc(the_date) between to_date('01-oct-'||the_year, 'dd-mon-yyyy')
and to_date('31-mar-'||(the_year+1), 'dd-mon-yyyy')
and dte < to_date('01-oct-'||the_year, 'dd-mon-yyyy')
then
dte
end) max_date
from table_a a
cross join (select v_inp_date the_date,
to_char(add_months(v_inp_date,-3), 'yyyy') the_year
from dual) dte;
入力した日付はdte
一部にあります
v_inp_date
と
to_char(add_months(v_inp_date,-3), 'yyyy') the_year
例:小さなテスト:
SQL> create table table_a(dte date);
Table created.
SQL> insert into table_a values(to_date('28-sep-2012', 'dd-mon-yyyy'));
1 row created.
SQL> insert into table_a values(to_date('29-sep-2012', 'dd-mon-yyyy'));
1 row created.
SQL> insert into table_a values(to_date('28-mar-2012', 'dd-mon-yyyy'));
1 row created.
SQL> insert into table_a values(to_date('29-mar-2012', 'dd-mon-yyyy'));
1 row created.
SQL> insert into table_a values(to_date('28-sep-2011', 'dd-mon-yyyy'));
1 row created.
SQL> insert into table_a values(to_date('27-mar-2011', 'dd-mon-yyyy'));
1 row created.
SQL> commit;
Commit complete.
SQL> var inpdate varchar2(20);
SQL> exec :inpdate := '27-dec-2012';
PL/SQL procedure successfully completed.
SQL> select max(case
2 when trunc(the_date) between to_date('01-apr-'||the_year, 'dd-mon-yyyy')
3 and to_date('30-sep-'||the_year, 'dd-mon-yyyy')
4 and dte < to_date('01-apr-'||the_year, 'dd-mon-yyyy')
5 then
6 dte
7 when trunc(the_date) between to_date('01-oct-'||the_year, 'dd-mon-yyyy')
8 and to_date('31-mar-'||(the_year+1), 'dd-mon-yyyy')
9 and dte < to_date('01-oct-'||the_year, 'dd-mon-yyyy')
10 then
11 dte
12 end) max_date
13 from table_a a
14 cross join (select to_date(:inpdate,'dd-mon-yyyy') the_date,
15 to_char(add_months(to_date(:inpdate,'dd-mon-yyyy'), -3), 'yyyy') the_year
16 from dual) dte;
MAX_DATE
---------
29-SEP-12
SQL> exec :inpdate := '17-aug-2012';
PL/SQL procedure successfully completed.
SQL> /
MAX_DATE
---------
29-MAR-12
SQL> exec :inpdate := '01-oct-2011';
PL/SQL procedure successfully completed.
SQL> /
MAX_DATE
---------
28-SEP-11
SQL> exec :inpdate := '01-apr-2011';
PL/SQL procedure successfully completed.
SQL> /
MAX_DATE
---------
27-MAR-11
SQL>