[英]Oracle -Assign Monday to Sunday week for the week number for calendar date
I have the data from 01 Jan 2019 - Present.我有 2019 年 1 月 1 日至今的数据。 I want to assign week numbers to the date column.
我想将周数分配给日期列。 The week should start on Monday and end on Sunday.
一周应该从星期一开始,到星期日结束。
But the function I use starts from 01 Jan 2019 (Tuesday) as week 1 start date for the following to_number(to_char(to_date(CALENDAR_D, 'dd-mm-yyyy'), 'ww'))但是我使用的 function 从 2019 年 1 月 1 日(星期二)开始作为以下 to_number(to_char(to_date(CALENDAR_D, 'dd-mm-yyyy'), 'ww')) 的第 1 周开始日期
If I replace 'ww' with 'iw', then it starts with first thursday of the week.如果我将 'ww' 替换为 'iw',那么它将从一周的第一个星期四开始。
I want the cycle to be Monday to Sunday with the first monday of the year 2019 to be my first day of my first week.我希望这个周期是周一到周日,2019 年的第一个周一是我第一周的第一天。
Expected Output预计 Output
Calendar_d Week Number
01/01/2019 Any Number (Tuesday)
01/02/2019 Any Number
01/03/2019 Any Number
01/04/2019 Any Number
01/05/2019 Any Number
01/06/2019 Any Number (Sunday)
01/07/2019 1
01/08/2019 1
01/09/2019 1
01/10/2019 1
01/11/2019 1
01/12/2019 1
01/13/2019 1
01/14/2019 2
You can use next_day()
and date arithmetic:您可以使用
next_day()
和日期算术:
select floor((datecol - next_day(date '2019-01-01', 'Monday')) / 7) + 1 as weeknum
Or, if this is in a table, you can use window functions:或者,如果这是在表中,您可以使用 window 函数:
select t.datecol,
sum(case when to_char(datecol, 'DY') = 'MON' then 1 else 0 end) over (partition by to_char(datecol, 'YYYY') order by t.datecol) as weeknum
from t
where t.datecol >= date '2019-01-01'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.