Getting the right week number of year

本秂侑毒 提交于 2019-12-25 00:11:59

问题


I want to get the right week number.

Using the to_char function, the function enumerate the week starting with mondey, but i want to get the week number considering that a week start on saturday.

Here is an example:

SELECT to_char('05-01-2013'::date,'daydd-mm-yyyy') as date_char, to_char('05-01-2013'::date,'IW') as week_number

Result:

"saturday 05-01-2013";"01"   

And should be:

"saturday 05-01-2013";"02"  

Is there any way to get it?

Config info: Postgresql 9.2 under Windows 7


回答1:


This will give the correct result:

with first_friday as (
    select extract(doy from min(a)::date) ff
    from generate_series('2013-01-01'::date, '2013-01-07', '1 day') s(a)
    where extract(dow from a) = 5
)
select
    to_char(a, 'day'),
    a::date "day",
    floor((extract(doy from a) - (select ff from first_friday) - 1) / 7) + 2 week_number
from generate_series('2013-01-01'::date, '2013-01-31', '1 day') s(a)
;
  to_char  |    day     | week_number 
-----------+------------+-------------
 tuesday   | 2013-01-01 |           1
 wednesday | 2013-01-02 |           1
 thursday  | 2013-01-03 |           1
 friday    | 2013-01-04 |           1
 saturday  | 2013-01-05 |           2
 sunday    | 2013-01-06 |           2
 monday    | 2013-01-07 |           2
 tuesday   | 2013-01-08 |           2
 wednesday | 2013-01-09 |           2
 thursday  | 2013-01-10 |           2
 friday    | 2013-01-11 |           2
 saturday  | 2013-01-12 |           3
 sunday    | 2013-01-13 |           3
 monday    | 2013-01-14 |           3
 tuesday   | 2013-01-15 |           3
 wednesday | 2013-01-16 |           3
 thursday  | 2013-01-17 |           3
 friday    | 2013-01-18 |           3
 saturday  | 2013-01-19 |           4
 sunday    | 2013-01-20 |           4
 monday    | 2013-01-21 |           4
 tuesday   | 2013-01-22 |           4
 wednesday | 2013-01-23 |           4
 thursday  | 2013-01-24 |           4
 friday    | 2013-01-25 |           4
 saturday  | 2013-01-26 |           5
 sunday    | 2013-01-27 |           5
 monday    | 2013-01-28 |           5
 tuesday   | 2013-01-29 |           5
 wednesday | 2013-01-30 |           5
 thursday  | 2013-01-31 |           5



回答2:


SELECT EXTRACT(WEEK FROM now()); will give the number 7, indicating that (at present) we are in the 7th week of the year. Postgres weeks start on Monday by default.

SELECT EXTRACT(WEEK FROM timestamp '2013-01-05'); will give 1 as only 5 days have passed since the start of the year.

Edit it appears that it only starts to count weeks from the time that the first full week has passed. Without writing your own function I would suggest going with this or using a different method to find it.




回答3:


You'll have to use extract(doy from ...) and extract(dow from ...) plus some math. Details in documentation.



来源:https://stackoverflow.com/questions/14815929/getting-the-right-week-number-of-year

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