We have an application which basically takes a companies fiscal year end, and walks backwards to gather ways using CONNECT BY LEVEL and looks up other data.
However, we found some companies that change their fiscal cycle causing the CONNECT BY LEVEL not to work properly. See the code, hopefully someone can give some creative idea?
First, see here how the company has December as their FY ending date, but then in 2018 changed to November:
11/30/2020
11/30/2019
11/30/2018
12/31/2017
12/31/2016
12/31/2015
The query developed looks like this:
SELECT TO_CHAR(a.fy,'MM/DD/YYYY') AS fy, DECODE(b.net_sales,NULL,'NA',b.net_sales) AS net_sales
FROM (SELECT ADD_MONTHS(LAST_DAY(TO_DATE(TO_CHAR(period_date), 'YYYYMM')), (LEVEL - 1) * -12) AS fy
FROM (SELECT m_ticker, period_type, MAX(period_date) AS period_date
FROM period_data
WHERE ticker = 'LUK'
AND period_type = 'A'
AND adjusted_revenue IS NOT NULL
GROUP BY ticker, period_type)
CONNECT BY LEVEL <= 5
ORDER BY 1 ASC) a,
(SELECT LAST_DAY(TO_DATE(TO_CHAR(period_date),'YYYYMM')) AS fy, TO_CHAR(NVL(adjusted_revenue,0)) AS net_sales
FROM period_data
WHERE ticker = 'LUK'
AND period_type = 'A'
ORDER BY period_date DESC NULLS LAST) b
WHERE a.fy = b.fy(+) ORDER BY 1 DESC;
As such, I get this output:
11/30/2018 5009.73
11/30/2017 NA
11/30/2016 NA
11/30/2015 NA
11/30/2014 NA
Because 2018 is 11/2018, but starting in 2017, it is 12/2017. The month changed, so subtracting 12 will not work anymore.......
Hoping to return values regardless of the value for the FY end......