我正在创建一个缓慢变化的维度表,以便记录某人何时更改了计划以及他们将其更改为
的内容这要求我存储星期几,其中包含与之关联的开始时间和结束时间,信息位于以UserID,星期几,StartTime,EndTime结构的表中,然后每天都有一行值。每个用户的一周。
最好的方法是将其展平并将其存储到一个表格中,以便我可以检索一周中任何一天的“开始”停止时间。我正在考虑一周中每一天的列,包括开始时间和停止时间。例如Monday_Start,Monday_End。一周中这一天的特定日期与此无关,因为rowstartdate列和rowenddate列记录了该方面。
答案 0 :(得分:0)
我认为没有理由在这里借助当前指示字段来使用数据库视图。
假设你有一个Type 2 Dimension:
UserID
,DayOfWeek
,StartWorkTime
,EndWorkTime
,EffectiveStartDate
,EffectiveEndDate
,Current -- Y/N Flags for being current or not current
您可以有效地创建一个数据库视图,将StartWorkTime和EndWorkTime转换为UserID和Current列。
CREATE VIEW [schema].[view_name] AS
SELECT
UserID
,[Current] -- Y = Yes / N = No
,MAX(CASE WHEN DayOfWeek = 2 THEN StartWorkTime ELSE NULL END) AS Mon_StartWorkTime
,MAX(CASE WHEN DayOfWeek = 2 THEN EndWorkTime ELSE NULL END) AS Mon_EndWorkTime
,MAX(CASE WHEN DayOfWeek = 3 THEN StartWorkTime ELSE NULL END) AS Tue_StartWorkTime
,MAX(CASE WHEN DayOfWeek = 3 THEN EndWorkTime ELSE NULL END) AS Tue_EndWorkTime
,MAX(CASE WHEN DayOfWeek = 4 THEN StartWorkTime ELSE NULL END) AS Wed_StartWorkTime
,MAX(CASE WHEN DayOfWeek = 4 THEN EndWorkTime ELSE NULL END) AS Wed_EndWorkTime
,MAX(CASE WHEN DayOfWeek = 5 THEN StartWorkTime ELSE NULL END) AS Thu_StartWorkTime
,MAX(CASE WHEN DayOfWeek = 5 THEN EndWorkTime ELSE NULL END) AS Thu_EndWorkTime
,MAX(CASE WHEN DayOfWeek = 6 THEN StartWorkTime ELSE NULL END) AS Fri_StartWorkTime
,MAX(CASE WHEN DayOfWeek = 6 THEN EndWorkTime ELSE NULL END) AS Fri_EndWorkTime
FROM [database].[schema].[table_name]
WHERE [Current] = 'Y'
GROUP BY
UserID
,[Current]
那么您的结果将是一名员工在某天工作0800到1600,其他工作日为1200天
UserID Current Mon_StartWorkTime Mon_EndWorkTime Tue_StartWorkTime Tue_EndWorkTime Wed_StartWorkTime Wed_EndWorkTime Thu_StartWorkTime Thu_EndWorkTime Fri_StartWorkTime Fri_EndWorkTime
1 Y 08:00:00.0000000 16:00:00.0000000 08:00:00.0000000 16:00:00.0000000 08:00:00.0000000 16:00:00.0000000 08:00:00.0000000 12:00:00.0000000 08:00:00.0000000 12:00:00.0000000