1

我一直在处理一个查询:

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

我该怎么做呢?

4

2 回答 2

2

改变

INNER JOIN Task T ON D.TaskId = T.PK_Task 

LEFT JOIN Task T ON D.TaskId = T.PK_Task 

当连接子句中的行不匹配时,LEFT 连接不会从 from 子句中的表中删除行。

此外,向 NonScrumStoryId 添加左连接,然后将您的选择更改为:

COALESCE(S.[Number], NonScrumStoryId.IncidentNumber)

COLLASCE 检查第一个参数是否为空,如果是则返回第二个参数。在这种情况下,如果故事为空,它将返回 IncidentNumber。

于 2013-09-17T21:03:21.413 回答
1

do You mean something like this:

CASE
WHEN S.[Number] is null
THEN NonScrumStory.IncidentNumber
ELSE S.[Number]
END
as Story

? Replace Your defintion for the Story-field with this and try...

于 2013-09-17T21:06:54.917 回答