Teradata SQL Week Number - Week 1 starting 1st Jan with weeks aligned to specific day of the week - sql

my first post on here so please be gentle...
I'm trying to create a week number variable in Teradata (SQL) that does the following:
Week 1 always starts on 1st January of the given year
Week numbers increment on the specified day of the week
For example: If Saturday was the specified day of the week:
2019-01-01 would be the start of week 1, 2019, changing to week 2 on 2019-01-05
2020-01-01 would be the start of week 1, 2020, changing to week 2 on 2020-01-04
I have come up wit the following based on an Excel function however it doesn't quite work as expected:
ROUND(((DATE_SPECIFIED - CAST(EXTRACT(YEAR FROM DATE_SPECIFIED) || '-01-01' AS DATE) + 1) - ((DATE_SPECIFIED - DATE '0001-01-06') MOD 7 + 1) + 10) / 7) AS REQUIRED_WEEK
The last digit of the section - DATE '0001-01-06' deals with the specified day of the week, where '0001-01-01' would be Monday.
This works in some cases however for some years, the first week number is showing as 0 where it should be 1, e.g. 1st Jan 2018 / 2019 are fine whereas 1st Jan 2020 is not.
Any ideas to correct this would be gratefully received.
Many thanks,
Mike

You can apply NEXT_DAY for both the specified date and Jan 1st of that year, e.g. for Saturday as week start:
(Next_Day(DATE_SPECIFIED,'SAT') - Next_Day(Trunc(DATE_SPECIFIED,'yyyy'),'SAT')) / 7 +1

Hmmm . . . I'm a bit week on Teradata functions. But the idea is to get the start of the second week. This follows the rule:
Jan 1 weekday (TD) 2nd week
Sunday 1 01-02
Monday 2 01-08
Tuesday 3 01-07
Wednesday 4 01-06
Thursday 5 01-05
Friday 6 01-04
Saturday 7 01-03
I think the following logic calculates this:
select t.*,
(case when td_day_of_week(cast(extract(year from DATE_SPECIFIED) || '-01-01' as date) ) = 1
then cast(extract(year from DATE_SPECIFIED) + '-01-02' as date)
else extract(year from DATE_SPECIFIED) + 10 - cast(td_day_of_week(cast(extract(year from DATE_SPECIFIED) || '-01-01') as date)
from t;
Then do you your week calculate either from the second week or subtract one more week to get when the first week really starts.

Related

SQL - POSTGRES - DATE_PART why is sql resulting in week 53 when it should be week 2

TABLE
INSERT INTO runners
("runner_id", "registration_date")
VALUES
(1, '2021-01-01'),
(2, '2021-01-03'),
(3, '2021-01-08'),
(4, '2021-01-15');
SQL Query
SELECT
DATE_PART('WEEK', R.registration_date) AS week_num,
COUNT(runner_id)
FROM
pizza_runner.runners R
GROUP BY
week_num
ORDER BY
week_num ASC;
I was expecting the query to return weeks 1 and 2 only but for some reason I am getting 53
]1
I was expecting the query to return weeks 1 and 2 only but for some reason I am getting 53
The documentation does a good job explaining the ISO rules for weeks - which Postgres follows:
The number of the ISO 8601 week-numbering week of the year. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year.
Using your dataset:
SELECT r.*,
extract(week from registration_date) AS week_num,
extract(isodow from registration_date) as day_of_week
FROM runners r
ORDER BY registration_date;
runner_id
registration_date
week_num
day_of_week
1
2021-01-01
53
5
2
2021-01-03
53
7
3
2021-01-08
1
5
4
2021-01-15
2
5
It turns out that January 3rd, 2021 was a Sunday (day of week 7). January 4st, 2021 was a Monday, and according to the ISO rules this is when the first week of that year began. Previous dates (January 3rd, 2nd, 1st, and so on) belong to the last week of 2020 (week 53), although the dates belong to year 2021.

January 1st = Week 1

The below gives me week numbers where week 1 starts on 1/4/2021
date_trunc('week', transaction_date) as week_number
How can I create a week_number where the week starts on January 1st and counts up 7 days for every week thereafter (for every year)?
And round up/down to 52 weeks at the end of the year?
Code attempted:
This doesn't give me the answer, but I'm thinking something like this might work...
ceil(extract(day from transaction_date)/7) as week_number
Expected Output:
transaction_date
week_number
1/1/2020
1
1/8/2020
2
...
...
12/31/2020
52
1/1/2021
1
1/8/2021
2
...
...
12/27/2021
52
12/28/2021
52
12/29/2021
52
12/30/2021
52
12/31/2021
52
1/1/2022
1
Thanks in advance!
A simple way is to use date arithmetic:
select 1 + (transaction_date - date_trunc('year', transaction_date)) / 7 as year_week
The below gives me week numbers where week 1 starts on 1/4/2021
It is the default behaviour and it is defined that way in ISO.
WEEK_OF_YEAR_POLICY
Type Session — Can be set for Account » User » Session
Description
Specifies how the weeks in a given year are computed.
Values
0: The semantics used are equivalent to the ISO semantics, in which a week belongs to a given year if at least 4 days of that week are in that year.
1: January 1 is included in the first week of the year and December 31 is included in the last week of the year.
Default 0 (i.e. ISO-like behavior)
It could be overrriden on multiple levels. The most granular is on the session level:
ALTER SESSION SET WEEK_OF_YEAR_POLICY = 1;
Then you could use the standard code:
SELECT date_trunc('week', transaction_date) as week_number
FROM ...;

Number of Weeks in a Month Vb.Net

Is there any DateTimePicker function to get the number of full weeks in a month?
Im looking for something like:
WeeksInMonth("01/01/2020") = 5
This will be the number of weeks per month manually calculated of this year:
Corrigendum: September has 4 weeks.
EDIT:
If the month starts in the mid of a week it should count as full week. It should also counts as full week if the month ends in the mid of a week.
From Monday to Sunday.
EDIT 2:
By mid week i mean if the week starts in Thursday it considers it as a complete week, because it was 4 days (thursday,friday,saturday and sunday) out of 7.
Same for the end, if the week end in thursday it will count as a complete Week, if the week finish in Monday, Tuesday or Wednesday it wont count because it only has 3 days out of 7.
Im taking in to consideration that my complete week starts in Monday and ends in Sunday.
Ex. January 2020 First complete week is: 30-dec-2019 to 05-jan-2020.
Another Example will be March 2020, Last complete week for me is 23-Mar-2020 to 29-Mar-2020, the 2 remaining days (30,31) would be part of April 1st complete week.
According to your logic, a week is in a month as long as its Thursday is in that month. If Thursday is in a month, then so are at least Monday, Tuesday, and Wednesday (if at the end) or Friday, Saturday, and Sunday (if at the beginning). So if you count the number of Thursdays in a month, you should get the number of weeks in that month.
Simply use LINQ to get all the days in a month, then take only days which are Thursday, then count those days
Public Function GetNumberOfWeeksInMonth(year As Integer, month As Integer) As Integer
Return Enumerable.Range(1, DateTime.DaysInMonth(year, month)).
Select(Function(d) New DateTime(year, month, d)).
Where(Function(d) d.DayOfWeek = DayOfWeek.Thursday).
Count()
End Function
' Concise version with Count with predicate
Public Function GetNumberOfWeeksInMonth(year As Integer, month As Integer) As Integer
Return Enumerable.Range(1, DateTime.DaysInMonth(year, month)).
Count(Function(d) (New DateTime(year, month, d)).DayOfWeek = DayOfWeek.Thursday)
End Function
Call it
For i = 1 To 12
Console.WriteLine($"{New DateTime(2020, i, 1):MMM}{vbTab}{GetNumberOfWeeksInMonth(2020, i)}")
Next
Jan 5
Feb 4
Mar 4
Apr 5
May 4
Jun 4
Jul 5
Aug 4
Sep 4
Oct 5
Nov 4
Dec 5
I assume this is something similar to the ISO 8601 scheme for year-weeks, where a week (a Mon-Sun period) falls into the year which contains it's Thursday.
Here a month contains a week if the Thursday of that week falls in the month.
Logically the number of such weeks in a month is the same as the number of Thursdays in that month.
You can derive this figure from the number of month-days in the month, and the week-day of the first month-day of the month.
A 28-day month always contains exactly 4 Thursdays. Months with more than 28 month-days may contain 5 Thursdays.
You can convert an ISO week-day number (Mon = 1, Sun = 7) to the following format like so: ((weekday + 2) Mod 7) - 2
Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = -2, Sat = -1, Sun = 0
Then add this adjusted week-day figure to the number of month-days. If the result exceeds 32, then it is a month containing 5 weeks.
So a 31-day month starting on a Monday, produces a figure of 32 (31 + 1). It contains 4 weeks.
A 31-day month starting on a Tuesday, produces a figure of 33 (31 + 2). It contains 5 weeks.
A 31-day month starting on a Friday, produces a figure of 29 (31 + (-2)). It contains 4 weeks.
Hope this helps.

Extract weekend days from date

I have date field and from that date field i am trying to extract only weekends i.e. in my case Saturday and Sunday is weekend.
So how can i extract weekends from date?
If below dates are in weekend then should be like this:
Date day working hours
01/01/2019
02/01/2019
03/01/2019
04/01/2019
05/01/2019 weekend 24
06/01/2019 weekend 87
07/01/2019
08/01/2019
09/01/2019
10/01/2019
Data link: https://www.dropbox.com/s/xaps82qyyo6i0fa/ar.xlsx?dl=0
You can use WeekDay functon. This function accepts date value/field and return the day of the week. The returned value is in dual format - day name and day number.
So you can create additional field that checks if the day number is >= 5 (day numbers are starting from 0 so Saturday = 5 and Sunday = 6)
RawData:
LOAD
AttendanceDay,
if(WeekDay(AttendanceDay) >= 5, 1, 0) as isWeekend,
Employee_ID,
WorkingHours
FROM
[..\Downloads\ar.xlsx]
(ooxml, embedded labels, table is Attendances_20191119_0838)
;
Resulted table after the reload:

Set first week of the year from the first day of the year until the following monday

I want that Oracle treats weeks the following way:
-The week starts on monday
-The first week of the year is from the first day of the year until the following monday
So for 2015 would be
01-jan-2015 (Thursday) would be 1
02-jan-2015 (Friday) would be 1
03-jan-2015 (Saturday) would be 1
04-jan-2015 (Sunday) would be 1
05-jan-2015 (Monday) would be 2
06-jan-2015 (Tuesday) would be 2
For 2017 would be
01-jan-2017 (Sunday) would be 1
02-jan-2017 (Monday) would be 2
03-jan-2017 (Tuesday) would be 2
04-jan-2017 (Wednesday) would be 2
and so on....
I need the numeration for the year so would go to 1 to 53-54
A little bit of arithmetics will probably do the trick (assuming d is your date):
select TRUNC((case to_char(d, 'DY')
when 'MON' then 6
when 'TUE' then 5
when 'WED' then 4
when 'THU' then 3
when 'FRI' then 2
when 'SAT' then 1
when 'SUN' then 0
end + to_number(to_char(d, 'DDD'))-1) / 7)+1
from v;
The case statement will build an offset according to the day of week
next I add the day of year
then I divide the result by 7 for the week number (starting at 0)
finally, the +1 will start numbering weeks at 1
Please take some time to experiment with it http://sqlfiddle.com/#!4/d41d8/38236 to check if I didn't make a stupid mistake as I didn't have time to test it thoughtfully. Sorry about that...
Use the ISO week date standard: 'iw'.
2014-01-01 was Wednesday actually, so:
select
to_number(to_char(date'2014-01-05', 'iw')) as weeknum,
to_char(date'2014-01-05', 'Day') as day
from dual;
WEEKNUM DAY
----------- ---------
1 Sunday
select
to_number(to_char(date'2014-01-06', 'iw')) as weeknum,
to_char(date'2014-01-06', 'Day') as day
from dual;
WEEKNUM DAY
----------- ---------
2 Monday