我正在尝试创建一份报告,概述一周内所有168小时的时间段供人们注册。下面的查询列出了用户填写的基本数据,但我希望看到也没有注册的插槽。
提前致谢!
SELECT
p.[FirstName]
, p.[LastName]
, ( SELECT TOP 1 [Value]
FROM [AttributeValue] av
INNER JOIN [Attribute] a ON a.[Id] = av.[AttributeId]
AND a.[EntityTypeId] = 113
AND a.[EntityTypeQualifierColumn] = 'WorkflowTypeId'
AND a.[EntityTypeQualifierValue] = w.[WorkflowTypeId]
WHERE [EntityId] = w.[Id]
AND a.[Key] = 'DayOfTheWeek'
) AS [Day]
, (SELECT TOP 1 [Value]
FROM [AttributeValue] av
INNER JOIN [Attribute] a ON a.[Id] = av.[AttributeId]
AND a.[EntityTypeId] = 113
AND a.[EntityTypeQualifierColumn] = 'WorkflowTypeId'
AND a.[EntityTypeQualifierValue] = w.[WorkflowTypeId]
WHERE [EntityId] = w.[Id]
AND a.[Key] = 'Time'
) AS [Time]
FROM
[Workflow] w
INNER JOIN [PersonAlias] pa ON pa.Id = w.[InitiatorPersonAliasId]
INNER JOIN [Person] p ON p.[Id] = pa.[PersonId]
WHERE w.[WorkflowTypeId] = 1032
答案 0 :(得分:0)
您可以在一周中的日期之间构建所有小时,然后您可以使用当前查询进行左连接以获取所有未使用的插槽。
declare @startDate datetime ='2014-11-23'
declare @endDate datetime ='2014-11-30'
;with cte
as
(
your current query
)
, cte1
as
(
select datename(WEEKDAY, @startDate) as [day], datepart(hour, @startdate) as [time],
dateadd(hour, datediff(hour, 0, @startDate)+1, 0) as nextVal
union all
select datename(WEEKDAY, nextVal) as [day], datepart(hour, nextVal) as [time],
dateadd(hour, datediff(hour, 0, nextVal)+1, 0) as nextVal
from cte1
where nextVal<@endDate
)
select * from cte1
left join cte
on cte.day = cte1.day
and cte.time = cte1.time
OPTION (MAXRECURSION 168)
要使所有插槽不单独使用,可以添加cte.day为NULL条件的位置