1 月和 12 月日期的 Postgresql 周数相同
Postgresql Week number same for Jan and Dec dates
在 PostgreSQL 中,我尝试使用下面的查询将时间戳转换为周数。
select user_id,
time_stamp,
date(time_stamp),
EXTRACT(YEAR FROM time_stamp) as Year,
EXTRACT(MONTH FROM time_stamp) as Month,
EXTRACT(WEEK FROM time_stamp) as Week
from user_engagement
user_id | time_stamp | date | year | month | week
---------+---------------------+------------+------+-------+------
6282 | 2013-01-01 14:29:35 | 2013-01-01 | 2013 | 1 | 1
6282 | 2013-01-02 14:29:35 | 2013-01-02 | 2013 | 1 | 1
6282 | 2013-12-30 14:29:35 | 2013-12-30 | 2013 | 12 | 1
6282 | 2013-12-31 14:29:35 | 2013-12-31 | 2013 | 12 | 1
我惊讶地发现 2013 年 1 月和 12 月的日期周数相同。有人可以解释一下这里的逻辑吗?
我认为 documentation 很好地解释了这一点:
week
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.
In the ISO week-numbering system, it is possible for early-January
dates to be part of the 52nd or 53rd week of the previous year, and
for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and
2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is
part of the first week of 2013. It's recommended to use the isoyear
field together with week to get consistent results.
在 PostgreSQL 中,我尝试使用下面的查询将时间戳转换为周数。
select user_id,
time_stamp,
date(time_stamp),
EXTRACT(YEAR FROM time_stamp) as Year,
EXTRACT(MONTH FROM time_stamp) as Month,
EXTRACT(WEEK FROM time_stamp) as Week
from user_engagement
user_id | time_stamp | date | year | month | week
---------+---------------------+------------+------+-------+------
6282 | 2013-01-01 14:29:35 | 2013-01-01 | 2013 | 1 | 1
6282 | 2013-01-02 14:29:35 | 2013-01-02 | 2013 | 1 | 1
6282 | 2013-12-30 14:29:35 | 2013-12-30 | 2013 | 12 | 1
6282 | 2013-12-31 14:29:35 | 2013-12-31 | 2013 | 12 | 1
我惊讶地发现 2013 年 1 月和 12 月的日期周数相同。有人可以解释一下这里的逻辑吗?
我认为 documentation 很好地解释了这一点:
week
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.
In the ISO week-numbering system, it is possible for early-January dates to be part of the 52nd or 53rd week of the previous year, and for late-December dates to be part of the first week of the next year. For example, 2005-01-01 is part of the 53rd week of year 2004, and 2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is part of the first week of 2013. It's recommended to use the isoyear field together with week to get consistent results.