我正在构建一个相对复杂的查询:
SELECT ROW_NUMBER() OVER (
ORDER BY TSK.[Status] DESC
) AS 'RowNumber'
,PDT.[Name] AS Project
,(
CASE
WHEN (
STY.KanBanProductId IS NOT NULL
AND STY.SprintId IS NULL
)
THEN 'KanBan'
WHEN (
STY.KanBanProductId IS NULL
AND STY.SprintId IS NOT NULL
)
THEN 'Sprint'
END
) AS ProjectType
,STY.[Number] StoryNumber
,STY.Title AS StoryTitle
,TSK.[Name] AS Task
,CONVERT(VARCHAR(20), STY.Effort) AS StoryPoints
,CONVERT(VARCHAR(20), TSK.OriginalEstimateHours) AS TaskHoursEstimate
,ISNULL(SUM(DTH.[Hours]), 0) AS WorkHours
,MAX(CASE
WHEN DTH.Hours != 0
THEN DTH.ActivityDate
END) AS LatestActivityDate
,TSK.[Status] AS [Status]
FROM Task TSK
LEFT JOIN DailyTaskHours DTH ON TSK.PK_Task = DTH.TaskId
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
WHERE TSK.PointPerson LIKE @userParam
AND TSK.[Status] LIKE @statusParam
AND PDT.[Name] != 'Overhead Codes'
GROUP BY LEN(STY.[Number])
,STY.[Number]
,TSK.[Status]
,STY.Title
,PDT.[Name]
,TSK.CreateDate
,TSK.[Name]
,STY.KanBanProductId
,STY.SprintId
,TSK.OriginalEstimateHours
,STY.Effort
ORDER BY CASE TSK.[Status]
WHEN 'Not Started'
THEN 1
WHEN 'In Progress'
THEN 2
WHEN 'Impeded'
THEN 3
WHEN 'Done'
THEN 4
END
我想为同一个StoryNumber(STY。[Number])添加一个对TaskHoursEstimate列(TSK.OriginalEstimateHours)求和的行。以下是一个例子:
突出显示的部分显示了我的意思。我想添加一行,因为storyNumber对于这3行(SPK01-22)是相同的,所以每行中有48行(24 + 16 + 8)。
我如何实现这一目标?
答案 0 :(得分:0)
我能够弄清楚。我使用了SELECT子查询:
SELECT ROW_NUMBER() OVER (
ORDER BY TSK.[Status] DESC
) AS 'RowNumber'
,TSK.PointPerson AS PointPerson
,PDT.[Name] AS Project
,(
CASE
WHEN (
STY.KanBanProductId IS NOT NULL
AND STY.SprintId IS NULL
)
THEN 'KanBan'
WHEN (
STY.KanBanProductId IS NULL
AND STY.SprintId IS NOT NULL
)
THEN 'Sprint'
END
) AS ProjectType
,STY.[Number] StoryNumber
,STY.Title AS StoryTitle
,TSK.[Name] AS Task
,CONVERT(VARCHAR(20), STY.Effort) AS StoryPoints
,(
SELECT SUM(OriginalEstimateHours)
FROM Task TSK
LEFT JOIN Story STU ON TSK.StoryId = STU.PK_Story
LEFT JOIN Sprint SPT ON STU.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STU.ProductId = PDT.PK_Product
LEFT JOIN [User] USR ON TSK.PointPerson = USR.DisplayName
WHERE TSK.PointPerson LIKE @userParam
AND (
(
@orgTeamPK = '%'
AND (
USR.[OrganizationalTeamId] LIKE @orgTeamPK
OR USR.[OrganizationalTeamId] IS NULL
)
)
OR (
@orgTeamPK <> '%'
AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
)
)
AND STU.Number LIKE STY.Number
) AS StoryHoursEstimate
,CONVERT(VARCHAR(20), TSK.OriginalEstimateHours) AS TaskHoursEstimate
,ISNULL(SUM(DTH.[Hours]), 0) AS WorkHours
,MAX(CASE
WHEN DTH.Hours != 0
THEN DTH.ActivityDate
END) AS LatestActivityDate
,TSK.[Status] AS [Status]
FROM Task TSK
LEFT JOIN DailyTaskHours DTH ON TSK.PK_Task = DTH.TaskId
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
LEFT JOIN [User] USR ON TSK.PointPerson = USR.DisplayName
WHERE TSK.PointPerson LIKE @userParam
AND TSK.[Status] LIKE @statusParam
AND PDT.[Name] != 'Overhead Codes'
AND (
(
@orgTeamPK = '%'
AND (
USR.[OrganizationalTeamId] LIKE @orgTeamPK
OR USR.[OrganizationalTeamId] IS NULL
)
)
OR (
@orgTeamPK <> '%'
AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
)
)
AND TSK.Deleted IS NULL
AND (
(
STY.Number LIKE @search
OR STY.Number IS NULL
)
OR (
STY.Title LIKE @search
OR STY.Title IS NULL
)
OR (
TSK.NAME LIKE @search
OR TSK.NAME IS NULL
)
)
GROUP BY LEN(STY.[Number])
,STY.[Number]
,TSK.[Status]
,STY.Title
,PDT.[Name]
,TSK.PointPerson
,TSK.CreateDate
,TSK.[Name]
,STY.KanBanProductId
,STY.SprintId
,TSK.OriginalEstimateHours
,STY.Effort
ORDER BY CASE TSK.[Status]
WHEN 'Not Started'
THEN 1
WHEN 'In Progress'
THEN 2
WHEN 'Impeded'
THEN 3
WHEN 'Done'
THEN 4
END