Masalah dengan solusi yang Anda coba adalah Anda melewatkan satu langkah.
Jika kuartal Anda berbeda satu bulan dari kuartal "standar", maka - diberi tanggal, tanggal apa pun - Anda harus memajukan tanggal satu bulan, lalu terpotong menjadi seperempat dan kemudian mundur satu bulan. Anda melakukan langkah 2 dan 3 tetapi Anda melewatkan langkah 1.
with
inputs ( dt ) as (
select to_date('02-Jan-18', 'dd-Mon-yy') from dual union all
select to_date('02-Feb-18', 'dd-Mon-yy') from dual union all
select to_date('05-Mar-18', 'dd-Mon-yy') from dual union all
select to_date('08-Apr-18', 'dd-Mon-yy') from dual union all
select to_date('05-May-18', 'dd-Mon-yy') from dual union all
select to_date('05-Jun-18', 'dd-Mon-yy') from dual union all
select to_date('05-Jul-18', 'dd-Mon-yy') from dual union all
select to_date('09-Aug-18', 'dd-Mon-yy') from dual union all
select to_date('10-Sep-18', 'dd-Mon-yy') from dual union all
select to_date('11-Oct-18', 'dd-Mon-yy') from dual union all
select to_date('11-Nov-18', 'dd-Mon-yy') from dual union all
select to_date('11-Dec-18', 'dd-Mon-yy') from dual
)
select dt,
add_months(trunc(add_months(dt, 1), 'q'), -1) as qtr
from inputs;
DT QTR
--------- ---------
02-Jan-18 01-Dec-17
02-Feb-18 01-Dec-17
05-Mar-18 01-Mar-18
08-Apr-18 01-Mar-18
05-May-18 01-Mar-18
05-Jun-18 01-Jun-18
05-Jul-18 01-Jun-18
09-Aug-18 01-Jun-18
10-Sep-18 01-Sep-18
11-Oct-18 01-Sep-18
11-Nov-18 01-Sep-18
11-Dec-18 01-Dec-18