I have table in Oracle , where EOW columns indicate the end of week.
I want to write a query to get the nearest end of week date.
Table Cal
DAY DAY OFTHE WEEK EOW
20181026 FRI Y
20181027 SAT N
20181028 SUN N
20181029 MON N
20181030 TUE N -->
20181031 WED N
20181101 THU N
20181102 FRI Y -->
20181103 SAT N
So when I
select DAY , "logic" from cal where day = 20181030;
What should be "logic" so that I get the nearest end of week date , in this case
20181026.
Please help!!
Do you really need a fixed table for that? A CTE can easily create any calendar you want, so - I took that freedom to produce something like this.
I wrote it step-by-step so that you could follow its execution. Start from the first CTE (dates), then go to day_diff, and so forth). It seems that you are selecting the first FRI that precedes current date. Because, for 20181030, the nearest end-of-week isn't 20181026 (4 days to that Friday) but 20181102 (3 days to that Friday).
At the end, the result is
SQL> with dates as
2 (select
3 -- add "level" (sequence of numbers from 1 to 60) to 1st of previous month
4 trunc(add_months(sysdate, - 1), 'mm') + level - 1 datum,
5 -- convert that date into a day name (MON, FRI, ...)
6 to_char(trunc(add_months(sysdate, -1), 'mm') + level - 1, 'DY',
7 'NLS_DATE_LANGUAGE=ENGLISH') dan,
8 -- if day name is FRI, set EOW = Y. Else, it is N
9 case when to_char(trunc(add_months(sysdate, -1), 'mm') + level - 1, 'DY',
10 'NLS_DATE_LANGUAGE=ENGLISH') = 'FRI' then 'Y'
11 else 'N'
12 end eow
13 from dual
14 connect by level <= 60 -- my CTE will have 60 dates; yours can have any number
15 ),
16 day_diff as
17 (select datum, dan, eow,
18 datum - to_date('&par_datum', 'dd.mm.yyyy') diff
19 from dates
20 ),
21 diff_only_eow as
22 (select datum, dan, eow, diff,
23 row_number() over (order by diff desc) rn
24 from day_diff
25 where eow = 'Y'
26 and diff <= 0
27 )
28 select datum, dan, eow, diff, rn
29 from diff_only_eow
30 where rn = 1;
Enter value for par_datum: 30.10.2018
DATUM DAN E DIFF RN
-------- ------------ - ---------- ----------
20181026 FRI Y -4 1
SQL>
What I am guessing is,
If the date is 30-OCT-2018 (20181030), then you want last friday date as 26-OCT - 2018 (20181026).
Another Scenario :
If the date is 27-OCT-2018 (20181027), in that case also you want last friday date which is 26-OCT - 2018 (20181026).
If my this is guess is true then below query may work :
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181030','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
It will display output as :
LAST_FRIDAY
26-OCT-18
Which you can convert later in your required format.
Test Case 2
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
Output :
LAST_FRIDAY
02-NOV-18
Now breaking of above query :
WITH clause is used in CTE.
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
Here, TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY' - will convert 20181103 as 03-NOV-2018.
So the result from the WITH clause (Which is 03-NOV-2018) will be used in another query :
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
Here DATE_TEST is output from with clause. First
TO_DATE(DATE_TEST,'DD-MON-YY')-7
It is taking previous 7 days from the mentioned date (which is currently DATE_TEST : 03-NOV -2018) So It will take all the last 7 days from 03-NOV-2018.
Assuming :
DATE DAY Order
28-10-2018 SUN 1
29-10-2018 MON 2
30-10-2018 TUE 3
31-10-2018 WED 4
01-11-2018 THURS 5
02-11-2018 FRI 6
03-11-2018 SAT 7
We got all 7 days mentioned above.
next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY')
Now from next_day, we can get another day and here, we are asking for FRIDAY by mentioning it in the argument. So FRIDAY is 02-11-2018.
So the output will be 02-11-2018.
This may solve your query
select day, to_char(to_date(day,'YYYYMMDD'),'DY') f1,
case to_char(to_date(day,'YYYYMMDD'),'DY')
when 'FRI' then 0
when 'SAT' then -1
when 'SUN' then -2
when 'MON' then -3
when 'TUE' then 3
when 'WED' then 2
when 'THU' then 1
end as f2,
to_char(to_date(day,'YYYYMMDD')+ case to_char(to_date(day,'YYYYMMDD'),'DY')
when 'FRI' then 0
when 'SAT' then -1
when 'SUN' then -2
when 'MON' then -3
when 'TUE' then 3
when 'WED' then 2
when 'THU' then 1
end,'YYYYMMDD') as f3
from test_cal;
DAY F1 F2 F3
-------- --------- ---------- --------
20181026 FRI 0 20181026
20181027 SAT -1 20181026
20181028 SUN -2 20181026
20181029 MON -3 20181026
20181030 TUE 3 20181102
20181031 WED 2 20181102
20181101 THU 1 20181102
20181102 FRI 0 20181102
20181103 SAT -1 20181102
9 rows selected.
Related
Greetings for the day!
i have written a python script that will run a select query using oracle database and will share the result with users based on the result it gets from that query. My aim is to run it through task scheduler on which it should automatically adjust the date mentioned in sql query and should always pick the last business day means if its monday, it should run the query with asof day as friday, if tuesday then asod day as Monday and so on.
Note : the report in the query runs on T+1 basis means if asof date is 21st Apr 2022 means it's actual start time would be 22 Apr 2022, so when it will run on 25th Apr (Monday) the asof date would be 22nd Apr
select* from snap_states
where asof = trunc(sysdate)-1
and upper(system) like ('LOANSL%')
order by start_time;***
If you're skipping weekends, then you could
SQL> with datum (sys_date) as
2 (select date '2022-04-23' from dual union all -- Saturday
3 select date '2022-04-24' from dual union all -- Sunday
4 select date '2022-04-25' from dual union all -- Monday
5 select date '2022-04-26' from dual -- Tuesday
6 )
7 select to_char(sys_date, 'dd.mm.yyyy, Dy') sys_date,
8 trunc(sys_date - case to_char(sys_date, 'Dy', 'nls_date_language = english')
9 when 'Sun' then 2
10 when 'Mon' then 3
11 else 1
12 end) as prev_work_day
13 from datum;
SYS_DATE PREV_WORK_
------------------------ ----------
23.04.2022, Sat 22.04.2022
24.04.2022, Sun 22.04.2022
25.04.2022, Mon 22.04.2022
26.04.2022, Tue 25.04.2022
SQL>
Applied to your query:
select *
from snap_states
where asof = trunc(sysdate - case to_char(sysdate, 'Dy', 'nls_date_language = english')
when 'Sun' then 2
when 'Mon' then 3
else 1
end)
and upper(system) like 'LOANSL%'
order by start_time;
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')
How can i get the date of the first business day of a given date .
For example:
01-AUG-21 is Sunday, so the first business day is 02-AUG-21 .
You can use a simple case statement in SQL or PL/SQL. In the example below, just replace SYSDATE + LEVEL with the date you would like to use.
SELECT SYSDATE + LEVEL AS some_date,
TO_CHAR (SYSDATE + LEVEL, 'DY') AS some_day_of_week,
SYSDATE
+ LEVEL
+ CASE TO_CHAR (SYSDATE + LEVEL, 'DY') WHEN 'SAT' THEN 2 WHEN 'SUN' THEN 1 ELSE 0 END AS business_day
FROM DUAL
CONNECT BY LEVEL <= 14;
SOME_DATE SOME_DAY_OF_WEEK BUSINESS_DAY
____________ ___________________ ________________
02-JUL-21 FRI 02-JUL-21
03-JUL-21 SAT 05-JUL-21
04-JUL-21 SUN 05-JUL-21
05-JUL-21 MON 05-JUL-21
06-JUL-21 TUE 06-JUL-21
07-JUL-21 WED 07-JUL-21
08-JUL-21 THU 08-JUL-21
09-JUL-21 FRI 09-JUL-21
10-JUL-21 SAT 12-JUL-21
11-JUL-21 SUN 12-JUL-21
12-JUL-21 MON 12-JUL-21
13-JUL-21 TUE 13-JUL-21
14-JUL-21 WED 14-JUL-21
15-JUL-21 THU 15-JUL-21
Here is a PL/SQL example of the same logic
DECLARE
FUNCTION get_business_day (p_date DATE)
RETURN DATE
IS
BEGIN
RETURN TRUNC (
p_date
+ CASE TO_CHAR (p_date, 'DY')
WHEN 'SAT' THEN 2
WHEN 'SUN' THEN 1
ELSE 0
END);
END;
BEGIN
DBMS_OUTPUT.put_line (get_business_day (p_date => SYSDATE));
END;
/
Here's one option: as it is only about 1 week, create a little one-week-calendar and fetch date which is larger than the one entered as a parameter, and which isn't a weekend.
with little_calendar as
(select to_date(:par_datum, 'dd.mm.yyyy') + level - 1 datum
from dual
connect by level <= 7
)
select min(datum)
from little_calendar
where datum > to_date(:par_datum, 'dd.mm.yyyy')
and to_char(datum, 'dy', 'nls_date_language = english')
not in ('sat', 'sun');
A few examples in SQL*Plus:
SQL> with little_calendar as
2 (select to_date('&&par_datum', 'dd.mm.yyyy') + level - 1 datum
3 from dual
4 connect by level <= 7
5 )
6 select min(datum)
7 from little_calendar
8 where datum > to_date('&&par_datum', 'dd.mm.yyyy')
9 and to_char(datum, 'dy', 'nls_date_language = english')
10 not in ('sat', 'sun');
Enter value for par_datum: 01.07.2021 --> the 1st working day after 01.07.2021 (Thursday) ...
MIN(DATUM)
---------------
02.07.2021, fri --> ... is 02.07.2021 (Friday)
SQL> undefine par_datum
SQL> /
Enter value for par_datum: 02.07.2021 --> working day that follows 02.07.2021 (Friday) ...
MIN(DATUM)
---------------
05.07.2021, mon --> ... is 05.07.2021 (Monday)
SQL>
My query return all days in month.
SELECT
EXTRACT( DAY FROM day ) ||' '|| substr(TO_CHAR( day, 'fmDAY' ),0,3) AS day,
EXTRACT( DAY FROM day ) as day_id
FROM (
WITH temp ( col ) AS (
SELECT to_date(2, 'mm') --2 is February
FROM dual
)
SELECT col + level - 1 AS day
FROM temp
CONNECT BY level <= last_day(col) - col + 1
ORDER BY day
)
How get all days from query where DAY_ID not in ==> (Select day_id from table1)
Eg. table1 return 5,10,15
Query resault need to display all days except 5,10,15
You can generate your calendar and then use NOT EXISTS:
WITH month ( col ) AS (
SELECT ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 2 - 1)
FROM DUAL
),
calendar ( day, end_day ) AS (
SELECT col, LAST_DAY(col)
FROM month
UNION ALL
SELECT day + INTERVAL '1' DAY, end_day
FROM calendar
WHERE day < end_day
)
SELECT EXTRACT( DAY FROM day ) ||' '|| TO_CHAR( day, 'fmDY' ) AS day,
EXTRACT( DAY FROM day ) as day_id
FROM calendar c
WHERE NOT EXISTS (
SELECT 1
FROM table1 t
WHERE c.day = t.day_id
)
ORDER BY c.day;
Which, for your sample data:
CREATE TABLE table1 ( day_id ) AS
SELECT DATE '2021-02-05' FROM DUAL UNION ALL
SELECT DATE '2021-02-10' FROM DUAL UNION ALL
SELECT DATE '2021-02-15' FROM DUAL;
Outputs:
DAY
DAY_ID
1 MON
1
2 TUE
2
3 WED
3
4 THU
4
6 SAT
6
7 SUN
7
8 MON
8
9 TUE
9
11 THU
11
12 FRI
12
13 SAT
13
14 SUN
14
16 TUE
16
17 WED
17
18 THU
18
19 FRI
19
20 SAT
20
21 SUN
21
22 MON
22
23 TUE
23
24 WED
24
25 THU
25
26 FRI
26
27 SAT
27
28 SUN
28
db<>fiddle here
One way to generate all rows in month for dates in another table is to use a recursive CTE:
create table table1 as
select date '2021-04-16' as day_in from dual;
with cte (dte) as (
select trunc(day_in, 'MON') as dte
from table1
union all
select dte + interval '1' day
from cte
where dte < last_day(dte)
)
select *
from cte;
Here is a db<>fiddle.
EDIT:
If you want the days not in a table, then use:
with cte (dte) as (
select date '2021-02-01' as dte
from dual
union all
select dte + interval '1' day
from cte
where dte < last_day(dte)
)
select dte
from cte
where not exists (select 1 from table1 t1 where t1.day_id = cte.dte);
You can also use not exists using your query, but I find the recursive CTE easier to follow -- and it is standard SQL.
name id
----------------
Mon 1
Thu 2
Wen 3
Thr 4
Fri 5
Sat 6
San 7
How get count day where id in eg. (1,2,3,4) and year is 2021
The result should be 208.
Actually, it is 208 for year 2021.
SQL> WITH
2 year AS (SELECT &par_year year FROM DUAL),
3 calendar
4 AS
5 ( SELECT TRUNC (TO_DATE (y.year, 'yyyy'), 'yyyy') + LEVEL - 1 datum
6 FROM year y
7 CONNECT BY LEVEL <=
8 ADD_MONTHS (TRUNC (TO_DATE (y.year, 'yyyy'), 'yyyy'), 12)
9 - TRUNC (TO_DATE (y.year, 'yyyy'), 'yyyy'))
10 SELECT SUM (CASE
11 WHEN TO_NUMBER (TO_CHAR (c.datum, 'd')) IN (1,
12 2,
13 3,
14 4)
15 THEN
16 1
17 ELSE
18 0
19 END) result
20 FROM calendar c;
Enter value for par_year: 2021
RESULT
----------
208
SQL> /
Enter value for par_year: 2020
RESULT
----------
210
SQL>
What does it do?
YEAR CTE contains year you're interested in
CALENDAR CTE creates all dates in that particular year
SUM function conditionally adds 1 if TO_CHAR(datum, 'd') is 1, 2, 3 or 4
that's all