我们有一个应用程序,该应用程序基本上需要公司的会计年度结束,然后向后退以使用CONNECT BY LEVEL收集方法并查找其他数据。
但是,我们发现一些公司改变了财务周期,导致CONNECT BY LEVEL无法正常工作。看代码,希望有人可以提出一些创意?
首先,在这里查看该公司如何将12月作为其财年结束日期,但是在2018年更改为11月:
11/30/2020
11/30/2019
11/30/2018
12/31/2017
12/31/2016
12/31/2015
开发的查询如下:
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;
这样,我得到以下输出:
11/30/2018 5009.73
11/30/2017 NA
11/30/2016 NA
11/30/2015 NA
11/30/2014 NA
因为2018年是11/2018,但是从2017年开始,它是12/2017。该月更改了,因此减去12将不再起作用。......
希望返回值,而与FY结束时的值无关……