下面的解決方案將輸入作爲綁定變量。它只在輸入完全符合該格式時才起作用(七個字符的字符串'Qx-yyyy'
,其中x
從1到4,yyyy
是有效年份),並且只有當「to-quarter」等於或晚於「from -25美分硬幣」;我的解決方案沒有做任何驗證以確保是這種情況,並且違反這些規則的行爲是未定義的(這意味着我沒有花時間測試任何「違反規則」的情況)。
with
prep (from_qtr, from_yr, to_qtr, to_yr) as (
select to_number(substr(:from_year, 2, 1)),
to_number(substr(:from_year, 4 )),
to_number(substr(:to_year , 2, 1)),
to_number(substr(:to_year , 4 ))
from dual
)
select add_months(to_date(to_char(from_yr) || '/' || to_char(3 * from_qtr - 2)
|| '/01', 'yyyy/mm/dd') - 1,
3 * (level - 1)
) as qtr_end_date
from prep
connect by level <= 1 + 4 * (to_yr - from_yr) + (to_qtr - from_qtr)
order by qtr_end_date
;
注:如果您無法連接使用按級別,你可以沒有它,但你需要一個機制來生成數字1到足夠高的限制。您可以硬編碼num
表(類似於下面的表),也可以使用某種機制從具有多行的表中讀取行號。這裏是一個可能的解決方案:
with
prep (from_qtr, from_yr, to_qtr, to_yr) as (
select to_number(substr(:from_year, 2, 1)),
to_number(substr(:from_year, 4 )),
to_number(substr(:to_year , 2, 1)),
to_number(substr(:to_year , 4 ))
from dual
),
num (rn) as (select rownum from all_tab_columns)
select add_months(to_date(to_char(from_yr) || '/' || to_char(3 * from_qtr - 2)
|| '/01', 'yyyy/mm/dd') - 1,
3 * (rn - 1)
) as qtr_end_date
from prep cross join num
where rn <= 1 + 4 * (to_yr - from_yr) + (to_qtr - from_qtr)
order by qtr_end_date
;
硬編碼的另一種方法是說num (rn) as select 1 from dual union all select 2 from dual union all select 3 from dual ...... )
,你需要有足夠的數量,以覆蓋所有可能的情況。
請包括您的表格中的一些示例數據以符合您的預期輸出。 –