我一直在处理一个查询:
SELECT D.PointPerson AS Person, R.[Name] AS Project, ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') AS Sprint, S.[Number] AS Story, T.[Name] AS Task,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 1 THEN D.[Hours] ELSE 0 END) AS Monday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 2 THEN D.[Hours] ELSE 0 END) AS Tuesday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 3 THEN D.[Hours] ELSE 0 END) AS Wednesday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 4 THEN D.[Hours] ELSE 0 END) AS Thursday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 5 THEN D.[Hours] ELSE 0 END) AS Friday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 6 THEN D.[Hours] ELSE 0 END) AS Saturday,
sum(CASE WHEN DatePart(dw, D.ActivityDate) = 7 THEN D.[Hours] ELSE 0 END) AS Sunday,
sum(D.[Hours]) AS Total
FROM DailyTaskHours D
INNER JOIN Task T ON D.TaskId = T.PK_Task
INNER JOIN Story S ON T.StoryId = S.PK_Story
LEFT JOIN Sprint P ON S.SprintId = P.PK_Sprint
INNER JOIN Product R ON S.ProductId = R.PK_Product
GROUP BY D.PointPerson, R.[Name], P.[Name], P.[Description], S.[Number], T.[Name]
ORDER BY CASE WHEN ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') = 'KanBan'
THEN 1
ELSE 0 END,
Project ASC,
Sprint ASC,
Story ASC,
Task ASC
如您所见,它将许多表格列聚集在一起。
有两个表与基表 (DailyTaskHours) 有关系:
目前,我的查询检索到上述模式右侧的所有关系。在所有这些情况下,DailyTaskHours 中的 NonScrumStoryId 列都是 NULL。作为同一查询的一部分,我想返回所有 DailyTaskHours 条目,包括 NonScrumSoryId 为 NULL 的条目。在这种情况下,与 Task、Story、Sprint 或 Product 表没有任何联系。
以下是该表目前的样子:
因此,如果该行是 NonScrumStory 行,即该行中的列不为空,它将填充下表,但 Project、Sprint 和 Task 都将为 NULL。故事将取自 NonScrumStory.IncidentNumber
我该怎么做呢?