How to SET DATEFIRST equal to Sunday in Amazon Redshift

风流意气都作罢 提交于 2019-12-25 01:29:47

问题


2016-01-01 is the week 1 of 2016, but also is the week 53 of 2015.

When I run SELECT DATE_PART(w, '2016-01-01') it returns 53, but when I run SELECT DATE_PART(w, '2016-01-04') it returns 1.

Most probably this is happening because Redshift sets Monday as the day 1 of the week, and not Sunday, as it should be.

Not sure if this will solve the problem, but what I need is to make 2016-01-01 as week 1 and from 2016-01-03 to 2016-01-09 as week 2 and so on...


回答1:


Create a custom function with this case statement or use it in your SQL statements:

case when DATE_PART('W', dt)>=51 and EXTRACT(DOY FROM dt) < 8 then 1 else DATE_PART('W', dt)+1 end week_no

It will start counting weeks on Jan 1st.

http://sqlfiddle.com/#!15/ccb90/9



来源:https://stackoverflow.com/questions/35416474/how-to-set-datefirst-equal-to-sunday-in-amazon-redshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!