使用SQL脚本填充时间维度-Teradata
各位 我正在尝试用SQL脚本填充时间维度表 预期输出如下所示 &下面是未提供正确输出的部分SQL脚本使用SQL脚本填充时间维度-Teradata,sql,teradata,Sql,Teradata,各位 我正在尝试用SQL脚本填充时间维度表 预期输出如下所示 &下面是未提供正确输出的部分SQL脚本 SEL calendar_date AS DATE_, RANK( ) OVER ( ORDER BY calendar_date) AS Date_Key, RANK( ) OVER ( ORDER BY EXTRACT(MONTH FROM CALENDAR_DATE)) AS Month_Key FROM SYS_CALENDAR.CALENDAR ORD
SEL calendar_date AS DATE_,
RANK( ) OVER ( ORDER BY calendar_date) AS Date_Key,
RANK( ) OVER ( ORDER BY EXTRACT(MONTH FROM CALENDAR_DATE)) AS Month_Key
FROM SYS_CALENDAR.CALENDAR
ORDER BY calendar_Date
你能帮我找到代码中的问题吗
由于代码在Month\u Key population中被阻塞,我还没有完成重新生成代码…将RANK()更改为Month\u Key(按提取顺序(从日历日期算起的月份))以使用densite\u RANK
,因为看起来一年中一个月的所有行都应该具有相同的键
SEL calendar_date AS DATE_,
RANK() OVER (ORDER BY calendar_date) AS Date_Key,
DENSE_RANK() OVER (ORDER BY EXTRACT(YEAR FROM CALENDAR_DATE),EXTRACT(MONTH FROM CALENDAR_DATE)) AS Month_Key
FROM SYS_CALENDAR.CALENDAR
ORDER BY calendar_Date
当你有sys\u calendar.calendar
select calendar_date
,day_of_calendar - 32872 as day_key
,month_of_calendar - 1080 as month_key
,quarter_of_calendar - 360 as quarter_key
,year_of_calendar - 1989 as year_key
from sys_calendar.calendar
-- where calendar_date between date '2015-09-25' and date '2015-10-05'
-- order by calendar_date
到底怎么了?您是否担心排名跳过数字或其他问题?基本上,我正在尝试使用SQL脚本构建一个时间维度,它也将对Teradata和SQL Server有用(尽管脚本中可能需要进行细微的语法更改)。。。。在提供的答案中,我不确定您为什么要从列中减去数字?例如:-日历的第天-32872作为第天,Teradata日历表从1900-01-01开始。减法是为了使它与您所需的结果相匹配
+---------------+---------+-----------+-------------+----------+
| calendar_date | day_key | month_key | quarter_key | year_key |
+---------------+---------+-----------+-------------+----------+
| 2015-09-25 | 9,399 | 309 | 103 | 26 |
| 2015-09-26 | 9,400 | 309 | 103 | 26 |
| 2015-09-27 | 9,401 | 309 | 103 | 26 |
| 2015-09-28 | 9,402 | 309 | 103 | 26 |
| 2015-09-29 | 9,403 | 309 | 103 | 26 |
| 2015-09-30 | 9,404 | 309 | 103 | 26 |
| 2015-10-01 | 9,405 | 310 | 104 | 26 |
| 2015-10-02 | 9,406 | 310 | 104 | 26 |
| 2015-10-03 | 9,407 | 310 | 104 | 26 |
| 2015-10-04 | 9,408 | 310 | 104 | 26 |
| 2015-10-05 | 9,409 | 310 | 104 | 26 |
+---------------+---------+-----------+-------------+----------+