Skip to Main Content
  • Questions
  • How to refer fiscal year calendar in queries?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Priyadarshni.

Asked: June 20, 2020 - 1:23 am UTC

Last updated: June 22, 2020 - 10:10 am UTC

Version: 11g

Viewed 1000+ times

You Asked

I am trying to create a calendar in Oracle PL/SQL, I need two calendars: Fiscal year calendar and normal calendar. I have created them using the code:

Fiscal year calendar:

WITH dts AS (SELECT TRUNC(to_date('&year', 'yyyy'), 'yyyy') + LEVEL + 181 AS dt
             FROM   dual
             CONNECT BY LEVEL <= to_char(TO_DATE(&YEAR || '1231', 'yyyymmdd'), 'ddd')),
           --  dt_mon AS (SELECT '7' from dual),
    dts2 AS (SELECT dt,
                    TRUNC(dt, 'mm') dt_mon,
                    TRUNC(dt, 'iw') dt_start_of_week,
                    to_char(dt, 'fmdd') day_of_month
             FROM   dts)

SELECT to_char(dt_mon, 'fmMonth') "MONTH",
       row_number() OVER (PARTITION BY to_char(dt_mon, 'fmMonth') ORDER BY dt_start_of_week) week_num,
       MAX(CASE WHEN dt = dt_start_of_week THEN day_of_month END) mon,
       MAX(CASE WHEN dt = dt_start_of_week + 1 THEN day_of_month END) tue,
       MAX(CASE WHEN dt = dt_start_of_week + 2 THEN day_of_month END) wed,
       MAX(CASE WHEN dt = dt_start_of_week + 3 THEN day_of_month END) thu,
       MAX(CASE WHEN dt = dt_start_of_week + 4 THEN day_of_month END) fri,
       MAX(CASE WHEN dt = dt_start_of_week + 5 THEN day_of_month END) sat,
       MAX(CASE WHEN dt = dt_start_of_week + 6 THEN day_of_month END) sun 
FROM   dts2
GROUP BY dt_mon,
         to_char(dt_mon, 'fmMonth'),
         dt_start_of_week
ORDER BY dt_mon, dt_start_of_week;


In my query, if I want to specify the fiscal year calendar, how can I do that? How to create a table with this calendar to refer in queries and If I want to store timeseries data, is there any provision in Oracle? Any help here would be appreciated. Thank you!!

and Connor said...

Not really because the definition of a "fiscal" year is totally at the whim of the organization that uses it.

I've worked for places where the fiscal year starts in April, others in July. Similarly, I've worked at places where the fiscal week starts on Monday, others on a Friday. I've even worked at places where the number of days in certain fiscal weeks changes from year to year, because the first day of the year starts on a different day, so they crammed an "8th day" into a week for business reporting.

There is no single "fiscal" year. Ultimately, most places do you what you've done above, namely have a table that stores the true calendar, its mapping to that company's fiscal calendar and use it (typically) as dimension table for reporting.

For example, here's an (anonymised) snippet from a client I worked with....

CREATE TABLE DATE_REPORTING
(
 ,CAL_DATE                   DATE         NOT NULL
 ,CAL_MONTH_START            DATE         NOT NULL
 ,CAL_MONTH_END              DATE         NOT NULL
 ,CAL_MONTH_OF_YEAR          NUMBER(2)    NOT NULL
 ,CAL_MONTH_OF_FIN_YEAR      NUMBER(2)    NOT NULL
 ,CAL_MONTH_NAME_ABBR        VARCHAR2(3)  NOT NULL
 ,CAL_MONTH_NAME             VARCHAR2(20) NOT NULL
 ,CAL_QTR_START              DATE         NOT NULL
 ,CAL_QTR_END                DATE         NOT NULL
 ,CAL_YEAR                   NUMBER(4)    NOT NULL
 ,CAL_DAY_OF_MONTH           NUMBER(3)    NOT NULL
--
 ,DAY_OF_WEEK_ABBR           VARCHAR2(3)  NOT NULL
 ,DAY_OF_WEEK                VARCHAR2(10) NOT NULL
 ,FIN_DAY_OF_WEEK            NUMBER(2)    NOT NULL
--  
 ,FIN_WEEK_OF_MONTH          NUMBER(2)    NOT NULL
 ,FIN_WEEK_OF_YEAR           NUMBER(2)    NOT NULL
 ,FIN_WEEK_START             DATE         NOT NULL
 ,FIN_WEEK_END               DATE         NOT NULL
 ,FIN_SETTLEMENT_PERIOD_SEQ  NUMBER(10)   
--
 ,FIN_MONTH_NAME             VARCHAR2(10) NOT NULL
 ,FIN_MONTH_NAME_ABBR        VARCHAR2(3)  NOT NULL
 ,FIN_MONTH_OF_YEAR          NUMBER(2)    NOT NULL
 ,FIN_MONTH_START            DATE         NOT NULL
 ,FIN_MONTH_END              DATE         NOT NULL
 ,FIN_MONTH_WEEKS            NUMBER       NOT NULL
--
 ,FIN_QTR_START              DATE         NOT NULL
 ,FIN_QTR_END                DATE         NOT NULL
--  
 ,FIN_YEAR                   NUMBER(4)    NOT NULL
 ,FIN_YEAR_NAME              VARCHAR2(8)  NOT NULL
 ,FIN_YEAR_START             DATE         NOT NULL
 ,FIN_YEAR_END               DATE         NOT NULL
...
...
...


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, June 22, 2020 - 5:55 am UTC

Thanks for the answer but how can I insert calendar data in this table? My fiscal year starts on July 1st and ends on June 30th.

Thanks..
Chris Saxon
June 22, 2020 - 10:10 am UTC

If your fiscal year is always 1 Jul - 30 June and you're currently at the end of FY2020, add six months to the calendar date to convert it to dates in 2020:

with rws as (
  select date'2019-06-30' + level dt 
  from   dual
  connect by level <= 367
)
  select dt, 
         add_months ( dt, 6 ) fy_dt, 
         extract ( year from trunc ( add_months ( dt, 6 ), 'y' ) ) fy
  from   rws;

DT                      FY_DT                        FY     
01-JUL-2019 00:00:00    01-JAN-2020 00:00:00       2020 
02-JUL-2019 00:00:00    02-JAN-2020 00:00:00       2020 
03-JUL-2019 00:00:00    03-JAN-2020 00:00:00       2020 
...
28-JUN-2020 00:00:00    28-DEC-2020 00:00:00       2020 
29-JUN-2020 00:00:00    29-DEC-2020 00:00:00       2020 
30-JUN-2020 00:00:00    31-DEC-2020 00:00:00       2020 
01-JUL-2020 00:00:00    01-JAN-2021 00:00:00       2021 


In general, add the number of days between the start of your fiscal year and the next calendar year to each date. Then extract the year from that to get the fiscal year-end.

Bear in mind there may be extra logic needed if fiscal years and calendar years contain different numbers of days. You need to speak with your business to find out what these rules are!

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library