Date calculation not working in Oracle SQL Case Statement? - sql

I am attempting to change the start of the week of a data set from Monday to the previous Friday. The overall data will be aggregated to a week level to look like this.
Week 1
Friday,
Saturday,
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday
The best I have come up with to do this is a case statement which sets Friday, Saturday and Sunday to next week's First day of the week (the system first day is Monday and last day is Sunday).
SELECT TRANSACTIONDATE,
CASE
WHEN TO_CHAR(TRANSACTIONDATE, 'DAY') IN ('FRIDAY', 'SATURDAY',
'SUNDAY') THEN (TRUNC(TRANSACTIONDATE, 'IW')+7)
ELSE TRUNC(TRANSACTIONDATE, 'IW')
END AS TEST
FROM TRANSACTIONS
My logic for this code is: if Fri, Sat or Sun then find the first day of the week and add 7 returning next week's first day of the week.
The problem is that the statement "TRUNC(TRANSACTIONDATE, 'IW')+7" is not adding 7 days to the week start.
When I query
select "TRUNC(TRANSACTIONDATE, 'IW')+7" from transactions
I get the next weeks week start which is correct.
This way all the days from Friday to Thursday are under the same week number.
Any ideas what's going wrong or if theres a better way to accomplish what I'm after?
Thanks,
Tom
Edit**
The first data set is what I would like at achieve,
Date Day Week
4/1/2018 Monday 1
4/2/2018 Tuesday 1
4/3/2018 Wednesday 1
4/4/2018 Thursday 1
4/5/2018 Friday 2
4/6/2018 Saturday 2
4/7/2018 Sunday 2
4/8/2018 Monday 2
4/9/2018 Tuesday 2
4/10/2018 Wednesday 2
4/11/2018 Thursday 2
So above you can see I want Firday/Saturday/Sunday to fall into the next week.
This is the current system dates/weeks does,
Date Day Week
4/1/2018 Monday 1
4/2/2018 Tuesday 1
4/3/2018 Wednesday 1
4/4/2018 Thursday 1
4/5/2018 Friday 1
4/6/2018 Saturday 1
4/7/2018 Sunday 1
4/8/2018 Monday 2
4/9/2018 Tuesday 2

The problem is TO_CHAR(TRANSACTIONDATE, 'DAY')
When you use 'DAY' then the weekday name is padded with space characters. Try SELECT '<'||TO_CHAR(DATE '2018-05-07', 'DAY')||'>' from dual; to see the effect.
Function TO_CHAR(..., 'DAY') returns the weekday name according to current user session NLS_DATE_LANGUAGE settings. This might be english or not.
Change your expression for example to
TO_CHAR(TRANSACTIONDATE, 'fmDAY', 'NLS_DATE_LANGUAGE=american') IN ('FRIDAY', 'SATURDAY', 'SUNDAY')

Related

how to use datediff to group Monday to Sunday when first date equals Sunday

Recognizing this is a common question, answered many times, I've not yet found a solution for my query.
My SQLServer ##DateFirst = 7 (Sunday) and I'm trying to Group a period of Invoice [Date]s by Week Starting Monday to Sunday.
The Query works fine but Sunday Dates are being grouped into the following Mondays Group. I understand why this is, but can't find a solution. I can't create Functions, so these methods don't help either.
SELECT
DATEADD(WEEK, DATEDIFF(WEEK,0, [DATE]), 0) AS 'DATE GROUP'
,[BRANCH]
,[SUNDTYPE]
,COUNT([INV_NUM]) AS 'Number of Sundrys'
,SUM([COST]) AS 'Sundry Rev'
FROM Invoice_Table
WHERE [DATE] BETWEEN '2022-03-07 00:00:00.000' AND '2022-03-13 00:00:00.000'
GROUP BY DATEADD(WEEK, DATEDIFF(WEEK,0, [DATE]), 0), SUNDTYPE, BRANCH
SQL Server is 2012
DATEADD and DATEDIFF to group all dates to the Monday of every week, Expecting Monday to Sunday Dates to all be Grouped together
You should be able to fake it out by shifting the date in question BACK 1 day. This way, what you are LOOKING FOR is
Mon Tue Wed Thu Fri Sat Sun
But the WEEK functionality is based on
Sun Mon Tue Wed Thu Fri Sat
So if your data based on this week for example is
Date IS What you ARE getting What you want
Fri Apr 1 Week X Week X
Sat Apr 2 Week X Week X
Sun Apr 3 Week Y Week X
Mon Apr 4 Week Y Week Y
Tue Apr 5 Week Y Week Y
Just subtract 1 day from the date in question that is being checked against the week function(), thus rolling all days back 1 makes Monday look like Sunday which is your correct break point, and Sunday rolls back to the previous week's Saturday.
DATEADD(WEEK, DATEDIFF(WEEK,0, dateadd( day, -1, [Date])), 0) AS 'DATE GROUP'
Then you can just re-add the date back 1 day to re-calibrate to Monday for display purposes.

Create a table to have only working days excluding weekends and public holidays

I have 2 tables.
1st of them consists of working days for each month (year = 2020):
Month
Date from
Date to
January
01/01/2020
31/01/2020
February
01/02/2020
29/02/2020
March
01/03/2020
31/03/2020
2nd of them has a dates with public holidays for 2020 with the following format:
Date
Week day
Code
Working hours
01/01/2020
Wednesday
Holiday
0
08/03/2020
Sunday
Holiday
0
01/09/2020
Tuesday
Holiday
0
24/12/2020
Thursday
Holiday
0
I would like to create 1 table (with the format of 2nd table) which has data only working days excluding public holidays from 2nd table and weekends from calendar for 2020.
Thank you in advance for your help.
Regards,
You can create the table using:
CREATE TABLE working_days (
day DATE PRIMARY KEY,
week_day VARCHAR2(9)
GENERATED ALWAYS AS (CAST(RTRIM(TO_CHAR(day, 'FMDay')) AS VARCHAR2(9))),
code VARCHAR2(20),
working_hours NUMBER(4,2)
);
Then insert data using:
INSERT INTO working_days (day, code, working_hours)
WITH days (dt, date_to) AS (
SELECT TRUNC(date_from),
date_to
FROM working_day_ranges
UNION ALL
SELECT dt + INTERVAL '1' DAY,
date_to
FROM days
WHERE dt + INTERVAL '1' DAY <= date_to
)
SELECT dt, 'Working Day', 23
FROM days d
WHERE NOT EXISTS ( -- Not a holiday
SELECT 1
FROM holidays h
WHERE d.dt = h.dt
)
AND dt - TRUNC(dt, 'IW') < 5 -- Not a Saturday or Sunday;
ORDER BY dt;
(Alter the expected working hours, etc. as suits you.)
Which, assuming your tables:
CREATE TABLE working_day_ranges (Date_from, Date_to) AS
SELECT DATE '2020-01-01', DATE '2020-01-31' FROM DUAL UNION ALL
SELECT DATE '2020-02-01', DATE '2020-02-29' FROM DUAL UNION ALL
SELECT DATE '2020-03-01', DATE '2020-03-31' FROM DUAL;
ALTER TABLE working_day_ranges
ADD month VARCHAR2(8)
GENERATED ALWAYS AS (CAST(RTRIM(TO_CHAR(date_from, 'FMMonth')) AS VARCHAR2(8)));
CREATE TABLE holidays (dt, Code, Working_hours) AS
SELECT DATE '2020-01-01', 'Holiday', 0 FROM DUAL UNION ALL
SELECT DATE '2020-03-08', 'Holiday', 0 FROM DUAL UNION ALL
SELECT DATE '2020-09-01', 'Holiday', 0 FROM DUAL UNION ALL
SELECT DATE '2020-12-24', 'Holiday', 0 FROM DUAL;
ALTER TABLE holidays
ADD week_day VARCHAR2(9)
GENERATED ALWAYS AS (CAST(RTRIM(TO_CHAR(dt, 'FMDay')) AS VARCHAR2(9)));
Then:
SELECT * FROM working_days;
Outputs:
DAY
WEEK_DAY
CODE
WORKING_HOURS
02-JAN-20
Thursday
Working Day
23
03-JAN-20
Friday
Working Day
23
06-JAN-20
Monday
Working Day
23
07-JAN-20
Tuesday
Working Day
23
08-JAN-20
Wednesday
Working Day
23
09-JAN-20
Thursday
Working Day
23
10-JAN-20
Friday
Working Day
23
13-JAN-20
Monday
Working Day
23
14-JAN-20
Tuesday
Working Day
23
15-JAN-20
Wednesday
Working Day
23
16-JAN-20
Thursday
Working Day
23
17-JAN-20
Friday
Working Day
23
20-JAN-20
Monday
Working Day
23
21-JAN-20
Tuesday
Working Day
23
22-JAN-20
Wednesday
Working Day
23
23-JAN-20
Thursday
Working Day
23
24-JAN-20
Friday
Working Day
23
27-JAN-20
Monday
Working Day
23
28-JAN-20
Tuesday
Working Day
23
29-JAN-20
Wednesday
Working Day
23
30-JAN-20
Thursday
Working Day
23
31-JAN-20
Friday
Working Day
23
03-FEB-20
Monday
Working Day
23
04-FEB-20
Tuesday
Working Day
23
05-FEB-20
Wednesday
Working Day
23
06-FEB-20
Thursday
Working Day
23
07-FEB-20
Friday
Working Day
23
10-FEB-20
Monday
Working Day
23
11-FEB-20
Tuesday
Working Day
23
12-FEB-20
Wednesday
Working Day
23
13-FEB-20
Thursday
Working Day
23
14-FEB-20
Friday
Working Day
23
17-FEB-20
Monday
Working Day
23
18-FEB-20
Tuesday
Working Day
23
19-FEB-20
Wednesday
Working Day
23
20-FEB-20
Thursday
Working Day
23
21-FEB-20
Friday
Working Day
23
24-FEB-20
Monday
Working Day
23
25-FEB-20
Tuesday
Working Day
23
26-FEB-20
Wednesday
Working Day
23
27-FEB-20
Thursday
Working Day
23
28-FEB-20
Friday
Working Day
23
02-MAR-20
Monday
Working Day
23
03-MAR-20
Tuesday
Working Day
23
04-MAR-20
Wednesday
Working Day
23
05-MAR-20
Thursday
Working Day
23
06-MAR-20
Friday
Working Day
23
09-MAR-20
Monday
Working Day
23
10-MAR-20
Tuesday
Working Day
23
11-MAR-20
Wednesday
Working Day
23
12-MAR-20
Thursday
Working Day
23
13-MAR-20
Friday
Working Day
23
16-MAR-20
Monday
Working Day
23
17-MAR-20
Tuesday
Working Day
23
18-MAR-20
Wednesday
Working Day
23
19-MAR-20
Thursday
Working Day
23
20-MAR-20
Friday
Working Day
23
23-MAR-20
Monday
Working Day
23
24-MAR-20
Tuesday
Working Day
23
25-MAR-20
Wednesday
Working Day
23
26-MAR-20
Thursday
Working Day
23
27-MAR-20
Friday
Working Day
23
30-MAR-20
Monday
Working Day
23
31-MAR-20
Tuesday
Working Day
23
db<>fiddle here

Pandas - Filtering out dates by day of week

I have a Dataframe that contains dates along with day of the week. I am trying to filter out dates that fall on Monday or Tuesday.
Given below is the view of my Dataframe and what I have tried doing thus far:
date, day_of_week
1/1/2018, Monday
1/2/2018, Tuesday
1/3/2018, Wednesday
Expected Output:
date, day_of_week
1/1/2018, Monday
1/2/2018, Tuesday
I tried to do the below to achieve the output:
df.index.day_of_week < 2
But it returns
AttributeError: 'Int64Index' object has no attribute 'day_of_week'
Try:
filtered_df = df.loc[df.day_of_the_week.isin(['Monday', 'Tuesday'])]
Output:
date day_of_the_week
0 1/1/2018 Monday
1 1/2/2018 Tuesday

SQL - pick current date from machine and compare the year

Scenario 1: Current Year
Always code needs to pick
The last Sunday of January for the current year. For ex(31-01-2016)
Current code - picks 1st of Jan 2016
convert(date,DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))
Scenario 2: Last Year
Always code needs to pick
The last Sunday of January for the Previous year. For ex(01-02-2015)
Current code - picks 1st of Jan 2015
convert(date,DATEADD(yy, DATEDIFF(yy, 0, dateadd(YEAR, - 1, getdate())), 0))
Instead of hard coding the date. I would like to pick date from machine and compare.
Week start from Sunday and ends Saturday. Any help please?
-- Sample Demonstrative Data/Test Results
Declare #YourTable table (SomeDate date)
Insert Into #YourTable values ('2000-06-15'),('2001-06-15'),('2002-06-15'),('2003-06-15'),('2004-06-15'),('2005-06-15'),('2006-06-15')
,('2007-06-15'),('2008-06-15'),('2009-06-15'),('2011-06-15'),('2012-06-15'),('2013-06-15'),('2014-06-15'),('2015-06-15'),('2016-06-15')
,('2017-06-15'),('2018-06-15'),('2019-06-15'),('2020-06-15')
-- To Confirm Results
Select Year = year(SomeDate)
,LastSundayDate = DateAdd(DD,-DatePart(DW,DateFromParts(Year(SomeDate),12,31))+1,DateFromParts(Year(SomeDate),12,31))
,LastSundayName = DateName(DW,DateAdd(DD,-DatePart(DW,DateFromParts(Year(SomeDate),12,31))+1,DateFromParts(Year(SomeDate),12,31)))
From #YourTable
Returns
Year LastSundayDate LastSundayName
2000 2000-12-31 Sunday
2001 2001-12-30 Sunday
2002 2002-12-29 Sunday
2003 2003-12-28 Sunday
2004 2004-12-26 Sunday
2005 2005-12-25 Sunday
2006 2006-12-31 Sunday
2007 2007-12-30 Sunday
2008 2008-12-28 Sunday
2009 2009-12-27 Sunday
2011 2011-12-25 Sunday
2012 2012-12-30 Sunday
2013 2013-12-29 Sunday
2014 2014-12-28 Sunday
2015 2015-12-27 Sunday
2016 2016-12-25 Sunday
2017 2017-12-31 Sunday
2018 2018-12-30 Sunday
2019 2019-12-29 Sunday
2020 2020-12-27 Sunday

sql code to print all the mondays of the year 2012

2012
January
2/1/2012
9/1/2012
16/1/2012
23/1/2012
30/1/2012
February
6/2/2012
.
.
.
.
and so on till decemeber.
the dates mentioned are mondays..How do i get the output in this format?? please help
One option
SQL> ed
Wrote file afiedt.buf
1 select next_day(trunc(sysdate,'YYYY') + 7*(level-1), 'MON' )
2 from dual
3* connect by level <= 53
SQL> /
NEXT_DAY(
---------
02-JAN-12
09-JAN-12
16-JAN-12
23-JAN-12
30-JAN-12
06-FEB-12
13-FEB-12
20-FEB-12
27-FEB-12
05-MAR-12
12-MAR-12
<<snip>>
03-DEC-12
10-DEC-12
17-DEC-12
24-DEC-12
31-DEC-12
53 rows selected.
Query:
select distinct next_day(to_date('01-01-2012','mm-dd-yyyy')-1+level,'Monday') All_MON from dual
connect by level <= floor(to_date('12-31-2011','mm-dd-yyyy') - to_date('01-01-2011','mm-dd-yyyy'))
order by All_MON;
Output:
01/02/2012 MONDAY
01/09/2012 MONDAY
01/16/2012 MONDAY
01/23/2012 MONDAY
01/30/2012 MONDAY
02/06/2012 MONDAY
02/13/2012 MONDAY
02/20/2012 MONDAY
02/27/2012 MONDAY
03/05/2012 MONDAY
03/12/2012 MONDAY
03/19/2012 MONDAY
03/26/2012 MONDAY
04/02/2012 MONDAY
04/09/2012 MONDAY
04/16/2012 MONDAY
04/23/2012 MONDAY
04/30/2012 MONDAY
05/07/2012 MONDAY
05/14/2012 MONDAY
05/21/2012 MONDAY
05/28/2012 MONDAY
06/04/2012 MONDAY
06/11/2012 MONDAY
06/18/2012 MONDAY
06/25/2012 MONDAY
07/02/2012 MONDAY
07/09/2012 MONDAY
07/16/2012 MONDAY
07/23/2012 MONDAY
07/30/2012 MONDAY
08/06/2012 MONDAY
08/13/2012 MONDAY
08/20/2012 MONDAY
08/27/2012 MONDAY
09/03/2012 MONDAY
09/10/2012 MONDAY
09/17/2012 MONDAY
09/24/2012 MONDAY
10/01/2012 MONDAY
10/08/2012 MONDAY
10/15/2012 MONDAY
10/22/2012 MONDAY
10/29/2012 MONDAY
11/05/2012 MONDAY
11/12/2012 MONDAY
11/19/2012 MONDAY
11/26/2012 MONDAY
12/03/2012 MONDAY
12/10/2012 MONDAY
12/17/2012 MONDAY
12/24/2012 MONDAY
12/31/2012 MONDAY