12月31日在Postgres中的一年中不一致的数字...... 1,52或53

时间:2015-12-16 19:35:15

标签: postgresql date

在逐年进行分组试验的过程中,我发现12月31日在Postgres中返回的一周中的一年随着意外的结果而变化(至少对我而言)。结果通常为52(好),有时为53,通常为1.

53我可以理解,因为它根据一周中的特定日期计算数周,因此按当天计算可能会持续53周。不太好,但可以理解。

但是为什么Postgres经常说12月31日是在一年的第一周?这有什么意义呢?

示例:

      SELECT EXTRACT(WEEK FROM DATE '2000-12-31') AS "Week",'Week2000' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2001-12-31') AS "Week",'Week2001' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2002-12-31') AS "Week",'Week2002' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2003-12-31') AS "Week",'Week2003' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2004-12-31') AS "Week",'Week2004' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2005-12-31') AS "Week",'Week2005' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2006-12-31') AS "Week",'Week2006' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2007-12-31') AS "Week",'Week2007' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2008-12-31') AS "Week",'Week2008' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2009-12-31') AS "Week",'Week2009' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2010-12-31') AS "Week",'Week2010' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2011-12-31') AS "Week",'Week2011' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2012-12-31') AS "Week",'Week2012' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2013-12-31') AS "Week",'Week2013' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2014-12-31') AS "Week",'Week2014' AS "Year"
UNION SELECT EXTRACT(WEEK FROM DATE '2015-12-31') AS "Week",'Week2015' AS "Year"
ORDER BY "Year"

结果:

Week   Year
-----  ---------
52     Week2000
1      Week2001
1      Week2002
1      Week2003
53     Week2004
52     Week2005
52     Week2006
1      Week2007
1      Week2008
53     Week2009
52     Week2010
52     Week2011
1      Week2012
1      Week2013
1      Week2014
53     Week2015

我已经测试过指定一个完整的时间戳,而不仅仅是日期,并且结果相同,无论我在时间戳中包含的是哪一天。

12月29日& 30也可以产生1,52或53个结果。

12月27日& 28可以导致52或53。

1 个答案:

答案 0 :(得分:3)

PostgreSQL使用ISO8601定义。这意味着星期一星期开始,一年的第一周是1月4日。如果1月4日星期二,那么12月31日就是那一年的第1周。