fiscal year date sequence generation - sql

I have a table called fiscal year with column start_date,end_date(empty table), I want to insert records for each fiscal year till 2060
FISCAL_YEAR Start dt is Jul 1st, End dt is Jun 31st of next year
what i tried
select add_months(start_date ,-6),add_months(start_date ,6)-1 from (
select to_date('20000101','yyyymmdd') start_date from dual )
basis
how do i generate this sequence till 2060
Decription start_date end_date
FISCAL YEAR 2000 7/1/1999 6/30/2000

SQL> select
2 to_date('01-07-' || (1999 + rownum), 'dd.mm.yyyy') start_date,
3 to_date('30-06-' || (2000 + rownum), 'dd.mm.yyyy') finish_date
4 from dual
5 connect by level <= 10;
START_DATE FINISH_DATE
----------- -----------
01.07.2000 30.06.2001
01.07.2001 30.06.2002
01.07.2002 30.06.2003
01.07.2003 30.06.2004
01.07.2004 30.06.2005
01.07.2005 30.06.2006
01.07.2006 30.06.2007
01.07.2007 30.06.2008
01.07.2008 30.06.2009
01.07.2009 30.06.2010
10 rows selected

You can do it like this:
SELECT
ADD_MONTHS(DATE '1999-07-01', 12*(LEVEL-1)) as fiscal_year_begin,
ADD_MONTHS(DATE '1999-07-01', 12*LEVEL) - INTERVAL '1' DAY AS fiscal_year_end
FROM dual
CONNECT BY LEVEL < 60;
FISCAL_YEAR_BEGIN FISCAL_YEAR_END
1999-07-01 2000-06-30
2000-07-01 2001-06-30
2001-07-01 2002-06-30
2002-07-01 2003-06-30
2003-07-01 2004-06-30
2004-07-01 2005-06-30
2005-07-01 2006-06-30
2006-07-01 2007-06-30
2007-07-01 2008-06-30
2008-07-01 2009-06-30
...

Related

How to get first date and last date of all twelve months for given year in Oracle PLSQL?

If i give input year like '2021' i need result as below
Month Start Date End Date
1 1/1/2021 31/01/2021
2 1/2/2021 28/01/2021
.
.
.
.
.
.
.
.
.
12 1/12/2021 31/12/2021
Basically, it is about the row generator technique; there are plenty of them, pick any you want. (Have a look at OraFAQ).
For example:
SQL> with mon as
2 (select add_months(trunc(to_date(&par_year, 'yyyy'), 'yyyy'), level - 1) val
3 from dual
4 connect by level <= 12
5 )
6 select to_char(val, 'mm') mon,
7 val start_date,
8 last_day(val) end_date
9 from mon
10 order by 1;
Enter value for par_year: 2021
MO START_DATE END_DATE
-- ---------- ----------
01 01/01/2021 31/01/2021
02 01/02/2021 28/02/2021
03 01/03/2021 31/03/2021
04 01/04/2021 30/04/2021
05 01/05/2021 31/05/2021
06 01/06/2021 30/06/2021
07 01/07/2021 31/07/2021
08 01/08/2021 31/08/2021
09 01/09/2021 30/09/2021
10 01/10/2021 31/10/2021
11 01/11/2021 30/11/2021
12 01/12/2021 31/12/2021
12 rows selected.
SQL>
You could also use directly the model clause for that purpose.
SELECT n
, TO_DATE(&the_year||lpad(f, 2, '0'), 'YYYYMM') start_dt
, last_day(TO_DATE(&the_year||lpad(f, 2, '0'), 'YYYYMM')) end_dt
FROM DUAL
MODEL
DIMENSION by (1 as n)
MEASURES (1 as f)
RULES (
f[FOR n FROM 1 TO 12 INCREMENT 1 ] = cv(n)
)
;
The advantage of the model clause is if you later want to get the every other month, you just need to change the increment from 1 to 2. Or if you are looking for the quarter months of the year (January, April, Jully, October), you just need to change increment from 1 to 3, and so on...
Just replace 2021 in the query for your year
with months (m) as (
select 1 from dual union all
select m + 1 from months where m < 12
)
select
to_date('2021' || '-' || to_char(m) || '-01', 'YYYY-MM-DD') as first_day,
last_day(to_date('2021' || '-' || to_char(m) || '-01', 'YYYY-MM-DD')) as last_day
from months
You can try on this db<>fiddle
Try below query
WITH cte_date as(
SELECT
LEVEL Month_No,
to_date(to_char(LEVEL||'-2021'),'MM-YYYY') Start_Date FROM dual
CONNECT BY LEVEL <=12
)
SELECT Month_No, Start_Date, LAST_DAY(Start_Date) End_Date
FROM cte_date;
I'm a fan of recursive CTEs because they are part of Standard SQL. I would phrase this as:
with months (month, startdate) as (
select 1 as month, date '2021-01-01'
from dual
union all
select month + 1, add_months(startdate, 1)
from months
where month < 12
)
select month, startdate, last_day(startdate) as enddate
from months;
If you need an input year, there are different ways to accomplish it. But a simple way is to change the second line to:
select 1 as month, to_date(:year || '0101', 'YYYYMMDD')

Oracle IF statement for Fixed Year

I have a weird condition while filtering oracle data:
if there are >=3 month already in the latest year, then only show data from this year and the last year. eg: now is May 2021, show 2020 Jan-Dec data and 2021 Jan-May data
if there are <3 month in the the latest year, then show data from the year before last year to now. eg: now is Jan 2021, show 2019 Jan-Dec data, 2020 Jan-Dec data and 2021 Jan
I have code like
SELECT * FROM DB
CASE
WHEN MDY_TIME BETWEEN
SELECT MAX(MDY_TIME, YEAR) AS MAX_DATE FROM DB)
WHERE MAX(MDY_TIME, MONTH)>='3'
THEN
I' sure I'm not using it right and I don't know if I'm even using the right field and don't know how to complete the conditions. This is my first time using SQL.
Data looks like:
I am assuming that the MDY_TIME column is a DATE data-type (if it is not then you should change it to be a DATE data-type.)
You want:
SELECT *
FROM DB
WHERE MDY_TIME >= CASE
WHEN EXTRACT( MONTH FROM SYSDATE ) < 3
THEN ADD_MONTHS( TRUNC( SYSDATE, 'YY' ), -24 )
ELSE ADD_MONTHS( TRUNC( SYSDATE, 'YY' ), -12 )
END
AND MDY_TIME < ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 )
Then for the sample data:
CREATE TABLE db (
mdy_time DATE,
year_month VARCHAR2(7)
GENERATED ALWAYS AS ( TO_CHAR( mdy_time, 'YYYY_MM' ) ),
year INTEGER
GENERATED ALWAYS AS ( EXTRACT(YEAR FROM mdy_time) ),
month INTEGER
GENERATED ALWAYS AS ( EXTRACT(MONTH FROM mdy_time) )
);
INSERT INTO db ( mdy_time )
SELECT ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 - LEVEL )
FROM DUAL
CONNECT BY LEVEL <= 36;
The output is:
MDY_TIME
YEAR_MONTH
YEAR
MONTH
2021-05-01 00:00:00
2021_05
2021
5
2021-04-01 00:00:00
2021_04
2021
4
2021-03-01 00:00:00
2021_03
2021
3
2021-02-01 00:00:00
2021_02
2021
2
2021-01-01 00:00:00
2021_01
2021
1
2020-12-01 00:00:00
2020_12
2020
12
2020-11-01 00:00:00
2020_11
2020
11
2020-10-01 00:00:00
2020_10
2020
10
2020-09-01 00:00:00
2020_09
2020
9
2020-08-01 00:00:00
2020_08
2020
8
2020-07-01 00:00:00
2020_07
2020
7
2020-06-01 00:00:00
2020_06
2020
6
2020-05-01 00:00:00
2020_05
2020
5
2020-04-01 00:00:00
2020_04
2020
4
2020-03-01 00:00:00
2020_03
2020
3
2020-02-01 00:00:00
2020_02
2020
2
2020-01-01 00:00:00
2020_01
2020
1
db<>fiddle here
I believe you're looking for something like the below
with max_date_sel as (select max(mdy_time) max_dt from DB)
select * from DB,
max_date_sel
where
case when extract(MONTH from max_dt) >= 3
and extract(YEAR from max_dt) - Year < 2 then 1
when extract(MONTH from max_dt) < 3
and extract(YEAR from max_dt) - Year < 3 then 1 end = 1
I would suggest:
where (extract(month from sysdate) < 3 and
mdy_time >= trunc(sysdate, 'YYYY') - interval '1' year
) or
mdy_time >= trunc(sysdate, 'YYYY')
If you want this based on the most recent year in the table, you can use window functions:
from (select db.*, max(mdy_time) over() as max_mdy_time
from db
) db
where (extract(month from max_mdy_time) < 3 and
mdy_time >= trunc(max_mdy_time, 'YYYY') - interval '1' year
) or
mdy_time >= trunc(max_mdy_time, 'YYYY')

How to find next Xth of month after a date

I have two parameters :
a date (Ex : 22/11/2016)
a day number (Ex : 25)
I want to find the next 25th of month after 22/11/2016: 25/11/2016
select trunc(date '2016-11-22', 'month') + 25
from dual;
trunc(date '2016-11-22', 'month') will return the first of the month, the + 25 will then add the desired 25 days.
If the meaning of the second parameter depends on the "comparison" date you can do something like this:
select case
when extract(day from date '2016-11-22') >= 25 then
add_months(trunc(date '2016-11-22', 'month'), 1) + 25
else trunc(date '2016-11-22', 'month') + 25
end as next_date
from dual;
Of course you would replace the hardcoded values for the date and the "number" of days with variables or column values.
This example:
with sample_data (the_date, num_days) as (
select date '2016-11-22', 25 from dual union all
select date ' 2016-11-26', 22 from dual union all
select date ' 2016-11-26', 3 from dual
)
select the_date, num_days,
case
when extract(day from the_date) >= num_days then
add_months(trunc(the_date, 'month'), 1) + num_days - 1
else trunc(the_date, 'month') + num_days - 1
end as next_date
from sample_data;
will return:
THE_DATE | NUM_DAYS | NEXT_DATE
------------+----------+------------
2016-11-22 | 25 | 2016-11-25
2016-11-26 | 22 | 2016-12-22
2016-11-26 | 3 | 2016-12-03
it can solve your problem:
select
(
case
when trunc (:yourdate-trunc(:yourdate,'month'))+1 <:urNum then
trunc(:yourdate,'month')+:urNum-1
else
trunc(last_day(:yourdate))+:urNum
end)
from dual;
You can use
LAST_DAY(<<a date>>) + <<a day number>> + 1
LAST_DAY gives you the last day of give, months (e.i. November, 30th), then add 1 day to get 1st of December plus your day number.
You could use this
WITH tmp AS
(
SELECT TO_DATE('22/11/2016', 'DD/MM/YYYY') date_col FROM DUAL
)
SELECT
CASE WHEN TO_CHAR(date_col,'DD') > '25'
THEN TO_DATE('25' || TO_CHAR(ADD_MONTHS(date_col, 1), '/MM/yyyy'), 'DD/MM/YYYY')
ELSE TO_DATE('25' || TO_CHAR(date_col, '/MM/yyyy'), 'DD/MM/YYYY') END date_col_new
FROM tmp;
If your input (25) is number, you could use TO_CHAR(your_input) instead of '25'
Here's one way:
WITH dates AS (SELECT to_date('30/11/2015', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT to_date('03/11/2015', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT to_date('31/10/2015', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT to_date('29/11/2015', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT to_date('31/01/2016', 'dd/mm/yyyy') dt FROM dual),
dom AS (SELECT 25 day_of_month FROM dual UNION ALL
SELECT 31 day_of_month FROM dual UNION ALL
SELECT 30 day_of_month FROM dual UNION ALL
SELECT 03 day_of_month FROM dual UNION ALL
SELECT 01 day_of_month FROM dual),
res AS (SELECT dates.dt starting_dt,
dom.day_of_month,
add_months(TRUNC(dates.dt, 'mm'),
CASE WHEN to_char(dates.dt, 'dd') >= dom.day_of_month
THEN 1
ELSE 0
END) month_of_end_dt
FROM dates
CROSS JOIN dom)
SELECT starting_dt,
day_of_month,
month_of_end_dt + least(day_of_month, to_number(to_char(last_day(month_of_end_dt), 'dd'))) - 1 next_date
FROM res
ORDER BY starting_dt, day_of_month;
STARTING_DATE DAY_OF_MONTH NEXT_DATE
------------- ------------ ----------
31/10/2015 1 01/11/2015
31/10/2015 3 03/11/2015
31/10/2015 25 25/11/2015
31/10/2015 30 30/11/2015
31/10/2015 31 30/11/2015
03/11/2015 1 01/12/2015
03/11/2015 3 03/12/2015
03/11/2015 25 25/11/2015
03/11/2015 30 30/11/2015
03/11/2015 31 30/11/2015
29/11/2015 1 01/12/2015
29/11/2015 3 03/12/2015
29/11/2015 25 25/12/2015
29/11/2015 30 30/11/2015
29/11/2015 31 30/11/2015
30/11/2015 1 01/12/2015
30/11/2015 3 03/12/2015
30/11/2015 25 25/12/2015
30/11/2015 30 30/12/2015
30/11/2015 31 30/11/2015
31/01/2016 1 01/02/2016
31/01/2016 3 03/02/2016
31/01/2016 25 25/02/2016
31/01/2016 30 29/02/2016
31/01/2016 31 29/02/2016
What this does is it first finds out the month that the new date is going to be in by comparing the day you're after with the date being compared with. (ie. if the day of the date you want to get to is already past the day of the starting date, add one to the month, otherwise add nothing).
Once you have that, it's a simple matter of adding the number of days you're trying to get to.
I have assumed that if the month doesn't have the full number of days (eg. February, April, June, September, November) then you'll want whatever the last day of that month is instead.
Therefore, we'll pick whichever is lower - the last day of the month or the day you want to get to. We have to subtract one from that result since we want to include the first of the month in the count.

How can I generate 4-week ranges of dates?

I have to generate date range for 1 year in gap of 4 weeks ( or 28 days ) from a fixed date backward and going forward. For example I have DATE '2016-02-20'. I need to generate the below.
Start date = Sunday , End-date = Saturday
No Start_date End_date
==== ========= =======
1 1/24/2016 2/20/2016
2 12/27/2015 1/23/2016
3 11/29/2015 12/26/2015
4 .....
13 2/22/2015 3/21/2015
14 1/25/2015 2/21/2015
But,when 03/20/2016(Sunday) comes,it should add
1. 2/21/2016 3/19/2016 & remove 14. 1/25/2015 2/21/2015
and so on after every 4 weeks.
I have written the below, but I need help to iterate in minimal code( if possible.)
SELECT LEVEL,
DATE '2016-02-20'-27*LEVEL-LEVEL+1 AS start_date,
DATE '2016-02-20'-28*(LEVEL-1) AS end_date
FROM DUAL
Connect BY LEVEL < 15;
It seems you want to have a rolling window of a year's worth of four-week ranges, based from the current date. To do that you need a fixed known period start (or end) date you can work from. Picking one that happens to be January 1st you can do:
SELECT DATE '2012-01-01' + (28 * (LEVEL - 1)) AS start_date,
DATE '2012-01-01' + (28 * LEVEL) - 1 AS end_date
FROM DUAL
CONNECT BY DATE '2012-01-01' + (28 * LEVEL) - 1 <= TRUNC(sysdate)
Which will find 54 periods up to today. On March 21st it will find 55 periods, etc. You only want those that are in the last year, so use that as an inline view and restrict the range:
SELECT ROW_NUMBER() OVER (ORDER BY start_date DESC) AS no, start_date, end_date
FROM (
SELECT DATE '2012-01-01' + (28 * (LEVEL - 1)) AS start_date,
DATE '2012-01-01' + (28 * LEVEL) - 1 AS end_date
FROM DUAL
CONNECT BY DATE '2012-01-01' + (28 * LEVEL) - 1 <= TRUNC(sysdate)
)
WHERE end_date >= ADD_MONTHS(TRUNC(sysdate), -12)
ORDER BY start_date DESC;
NO START_DATE END_DATE
---------- ---------- ----------
1 01/24/2016 02/20/2016
2 12/27/2015 01/23/2016
3 11/29/2015 12/26/2015
...
11 04/19/2015 05/16/2015
12 03/22/2015 04/18/2015
13 02/22/2015 03/21/2015
The ROW_NUMBER() just generates your NO column, as the LEVEL is now in the wrong order.
If you always want exactly 14 rows in the result set you can move the ROW_NUMBER() into the inline view:
SELECT no, start_date, end_date
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY LEVEL DESC) AS no,
DATE '2012-01-01' + (28 * (LEVEL - 1)) AS start_date,
DATE '2012-01-01' + (28 * LEVEL) - 1 AS end_date
FROM DUAL
CONNECT BY DATE '2012-01-01' + (28 * LEVEL) - 1 <= TRUNC(sysdate)
)
WHERE no <= 14
ORDER BY no;
NO START_DATE END_DATE
---------- ---------- ----------
1 01/24/2016 02/20/2016
2 12/27/2015 01/23/2016
3 11/29/2015 12/26/2015
...
12 03/22/2015 04/18/2015
13 02/22/2015 03/21/2015
14 01/25/2015 02/21/2015
14 rows selected

Insert weekly dates starting fridays

I'm trying to insert weekly dates in my table, the start date is always on Fridays and the end date is always on thursday. I'm using this code :
CREATE TABLE WEEK AS
WITH generator AS (
SELECT DATE '2015-01-02' + LEVEL - 1 dt
FROM dual
CONNECT BY LEVEL <= DATE '2016-01-21' - DATE '2015-01-02' + 1
)
SELECT to_char(dt, 'YYYY "SEM"IW') "KEY",
dt "DATE_START",
least(next_day(dt - 1, to_char(DATE '2015-01-08', 'DAY')),
last_day(dt)) "DATE_END"
FROM generator
WHERE to_char(dt, 'D') = to_char(DATE '2015-01-02', 'D');
The code is working for weeks on the same month, but if I have a starting date on a month and the finish date on the next month, there's no data inserting in my table.
For example :
Date_ START | DATE_END
29-05-2015 | 31-05-2015
05-06-2015 | 11-05-2015
Instead of 31-05-2015 I should have 04-06-2015.
I think the following is what you're after:
with generator as (select date '2015-05-29' + (level - 1)*7 dt
from dual
connect by level <= (date '2016-01-21' - date '2015-05-29')/7 + 1)
select to_char(dt, 'YYYY "SEM"IW') "KEY",
dt "DATE_START",
dt + 6 "DATE_END"
from generator;
KEY DATE_START DATE_END
---------- ---------- ----------
2015 SEM22 2015-05-29 2015-06-04
2015 SEM23 2015-06-05 2015-06-11
2015 SEM24 2015-06-12 2015-06-18
2015 SEM25 2015-06-19 2015-06-25
<snip>
2016 SEM01 2016-01-08 2016-01-14
2016 SEM02 2016-01-15 2016-01-21
This is assuming that the dates you have specified in the generator subquery have already been determined to be a Friday. Otherwise you could use something like trunc(<date> - 4, 'iw') + 4 or trunc(<date> + 3, 'iw') + 4 (depending on whether you want the previous or next Friday to be included for the date specified) to make sure that the seed date is definitely a Friday.
Maybe just an alternative you can try analytical function here. But as
suggested by Boniest "just adding days" will be better approach. Have
fun
WITH generator AS
(SELECT DATE '2015-01-02' + LEVEL - 1 dt
FROM dual
CONNECT BY LEVEL <= DATE '2016-01-21' - DATE '2015-01-02' + 1
)
-- select * from generator;
SELECT TO_CHAR(dt, 'YYYY "SEM"IW') "KEY",
dt "DATE_START",
lead(dt) over (ORDER BY (dt)) -1 "End Date"
FROM generator
WHERE TO_CHAR(dt, 'D') = TO_CHAR(DATE '2015-01-02', 'D');
----------------------------------OUTPUT-----------------------------------------
**KEY DATE_START End Date**
2015 SEM18 05/01/2015 05/07/2015
2015 SEM19 05/08/2015 05/14/2015
2015 SEM20 05/15/2015 05/21/2015
2015 SEM21 05/22/2015 05/28/2015
2015 SEM22 05/29/2015 06/04/2015
----------------------------------------------------------------------------------