[英]First Day of nth Week of the Year in Netezza
How can I find the date of the first day of the n
th week of the year? 如何找到一年中第
n
周第一天的日期? For example, all of the following queries: 例如,以下所有查询:
SELECT DATE_PART('WEEK', '2019-07-22 01:12:07'::timestamp);
SELECT DATE_PART('WEEK', '2019-07-23 01:12:07'::timestamp);
SELECT DATE_PART('WEEK', '2019-07-24 01:12:07'::timestamp);
give 30, because these three dates are in the 30th week of 2019. 给出30,因为这三个日期是在2019年的第30周。
My question is how I can do the opposite of this. 我的问题是我该如何做相反的事情。 Weeks start on Mondays in my version of SQL (not a problem if your starts on Sunday - I can do the adjustment) therefore the first day of week 30 is 2019-07-22 (today, which is Monday).
在我的SQL版本中,每周从星期一开始(如果您从星期日开始,这不是问题-我可以进行调整),因此,第30周的第一天是2019-07-22(今天,星期一)。 How can I enter the week number, 30, and get the first day of that week, ie 2019-07-22 (or 07-21, if the first day of a week is a Sunday in your SQL)
如何输入星期数30,并获取该星期的第一天,即2019-07-22(如果您的SQL中一周的第一天是星期日,则为07-21)
You have to find the date of the last monday of the previous year and add 7 * {week-number} days to it 您必须找到上一年的最后一个星期一的日期,并向其添加7 * {week-number}天
PS: don't forget to account for years that have a 29th of February :) PS:别忘了记下2月29日的年份:)
Turns out, the solution is actually pretty easy. 事实证明,该解决方案实际上非常简单。 I had to ask it to come up with a solution I guess.
我不得不要求它想出一个解决方案。 Here it is, for the week number 30:
在这里,第30周:
SELECT ('2019-01-01'::timestamp + (30-1)*7 - (DATE_PART('DOW', DATE '2019-01-01')-2))
-- Result: 2019-07-22
I basically start from the first day of the year, add (n-1)*7
, and adjust for the first day of the year not being a Monday. 我基本上从一年的第一天开始,加上
(n-1)*7
,然后调整为一年的第一天,而不是星期一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.