当使用以下查询返回一周中的天数时,我希望仅显示特定日期(例如,星期一)并计算星期一'归还。
SELECT
w.[Id]
, w.[Name]
, [Status]
, [InitiatorPersonAliasId]
, 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 :(得分:1)
你可以写成:
;With cte_DayCount as
(
SELECT av.[Value] as DayCount,w.[Id] AS Id
FROM [AttributeValue] av
INNER JOIN [Workflow] w ON av.[EntityId] = w.[Id]
INNER JOIN [Attribute] a ON a.[Id] = av.[AttributeId]
AND a.[EntityTypeId] = 113
AND a.[EntityTypeQualifierColumn] = 'WorkflowTypeId'
AND a.[EntityTypeQualifierValue] = w.[WorkflowTypeId]
AND a.[Key] = 'DayOfTheWeek'
WHERE [Value] Like '%Monday%' -- Add day value here
GROUP BY w.[Id], av.Value
)
SELECT COUNT(dc.DayCount) AS Total
FROM
[Workflow] w
INNER JOIN [PersonAlias] pa ON pa.Id = w.[InitiatorPersonAliasId]
INNER JOIN [Person] p ON p.[Id] = pa.[PersonId]
INNER JOIN cte_DayCount dc ON DC.Id = w.[Id]
WHERE w.[WorkflowTypeId] = 1032