如何计算日期维度的iso_week_start_date和iso_week_end_date?

时间:2017-10-10 13:31:25

标签: data-modeling data-warehouse dimension snowflake-datawarehouse

作为日历维度的一部分,我需要填充iso_week_start_date,iso_week_end_date和iso_week_number。我已经能够构建除这些字段之外的日历维度。谢谢。

1 个答案:

答案 0 :(得分:0)

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周有一些奇怪的语义,请参阅文档以获取更多详细信息。