这是我的[资源表]我正在获取我的数据:
Start Date Time From Time To Time From To Training Room
2011-05-24 00:00:00.000 8:00 23:00 8:00 - 23:00 Room1
2011-05-25 00:00:00.000 1:00 23:00 1:00 - 23:00 Room1
2011-05-26 00:00:00.000 1:00 23:00 1:00 - 23:00 Room1
2011-05-04 00:00:00.000 20:00 23:00 20:00 - 23:00 Room2
2011-05-05 00:00:00.000 20:00 22:00 20:00 - 22:00 Room2
2011-05-20 00:00:00.000 20:00 23:00 20:00 - 23:00 Room3
2011-05-21 00:00:00.000 17:00 19:00 17:00 - 19:00 Room2
我想要的是填写此表[FACT培训]:
但是我在填充没有空值的表时遇到了问题。 由于我想在每天的列中插入数据,因此只插入特定的dayofweek名称:
WHERE datename(dw,[Start Date]) = 'Monday'
WHERE datename(dw,[Start Date]) = 'Tuesday'
etc...
所以我的插件看起来像这样:
但需要每天过滤......
答案 0 :(得分:0)
看一下你所描述的问题,我开始时只将列值放入一周中正确的日期,这将创建7行,每行只填充[Start Date]
落下的那一天的列。 / p>
SELECT
[Monday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Monday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Tuesday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Tuesday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Wednesday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Wednesday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Thursday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Thursday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Friday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Friday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Saturday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Saturday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
,[Sunday] =
CASE WHEN DATENAME(DW,[Start Date]) = 'Sunday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END
FROM
[Resource table]
但后来我想,您可能希望在一行中显示所有这些,在这种情况下,您需要按特定值或值对它们进行分组。我不知道SQL Aggregate函数用于为组创建列值的连接列表,因此您可能需要创建一个,但是您可以通过培训室对其进行分组,如果每次培训每天只有一个条目可以使用类似下面的房间(SQL纯粹主义者现在可能想要把目光移开):
SELECT
[Week Id] = CONVERT(VARCHAR,DATEPART(YEAR,[Start Date])) + '-' + CONVERT(VARCHAR,DATEPART(WEEK,[Start Date]))
,[Training Room]
,[Monday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Monday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Tuesday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Tuesday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Wednesday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Wednesday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Thursday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Thursday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Friday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Friday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Saturday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Saturday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
,[Sunday] = MAX(CASE WHEN DATENAME(DW,[Start Date]) = 'Sunday' THEN ISNULL([Training Room],'') + N' - ' + [Time From To] ELSE '' END)
FROM
[Resource table]
GROUP BY
CONVERT(VARCHAR,DATEPART(YEAR,[Start Date])) + '-' + CONVERT(VARCHAR,DATEPART(WEEK,[Start Date]))
,[Training Room]
您可以随时省略我按([Week Id]
和[Training Room]
分组的两个初始列的声明,因为您无需显示它们以便查看它们。