如何计算日期维度的 iso_week_start_date 和 iso_week_end_date?
How to calculate iso_week_start_date and iso_week_end_date for date dimension?
作为日历维度的一部分,我需要填充 iso_week_start_date、iso_week_end_date 和 iso_week_number。除了这些字段,我已经能够构建日历维度。谢谢。
Snowflake 提供 DATE_TRUNC(WEEK, ..)
让您获得 ISO 周的第一天。然后加上 6 天就是最后一天。还有 DATE_EXTRACT(WEEK, ..)
(或简称 WEEK(..)
)
例如:
select extract(week, '2017-10-10'::date), date_trunc(week, '2017-10-10'::date), dateadd(day, 6, date_trunc(week, '2017-10-10'::date));
-----------------------------------+--------------------------------------+-------------------------------------------------------+
EXTRACT(WEEK, '2017-10-10'::DATE) | DATE_TRUNC(WEEK, '2017-10-10'::DATE) | DATEADD(DAY, 6, DATE_TRUNC(WEEK, '2017-10-10'::DATE)) |
-----------------------------------+--------------------------------------+-------------------------------------------------------+
41 | 2017-10-09 | 2017-10-15 |
-----------------------------------+--------------------------------------+-------------------------------------------------------+
请记住,ISO 周有一些奇怪的语义,请参阅文档了解更多详细信息。
作为日历维度的一部分,我需要填充 iso_week_start_date、iso_week_end_date 和 iso_week_number。除了这些字段,我已经能够构建日历维度。谢谢。
Snowflake 提供 DATE_TRUNC(WEEK, ..)
让您获得 ISO 周的第一天。然后加上 6 天就是最后一天。还有 DATE_EXTRACT(WEEK, ..)
(或简称 WEEK(..)
)
例如:
select extract(week, '2017-10-10'::date), date_trunc(week, '2017-10-10'::date), dateadd(day, 6, date_trunc(week, '2017-10-10'::date));
-----------------------------------+--------------------------------------+-------------------------------------------------------+
EXTRACT(WEEK, '2017-10-10'::DATE) | DATE_TRUNC(WEEK, '2017-10-10'::DATE) | DATEADD(DAY, 6, DATE_TRUNC(WEEK, '2017-10-10'::DATE)) |
-----------------------------------+--------------------------------------+-------------------------------------------------------+
41 | 2017-10-09 | 2017-10-15 |
-----------------------------------+--------------------------------------+-------------------------------------------------------+
请记住,ISO 周有一些奇怪的语义,请参阅文档了解更多详细信息。