UNION を使用して接続された 2 つのクエリを組み合わせた派生テーブルがあります。表が導出されているのは、同じ人の合計が2回出てくる場合があるからです。この場合、whoes の合計が大きい行のみを表示します。
したがって、派生テーブルには次のコマンドがあります。
MAX(DerivedTable.TotalHours)
SELECT コマンドの一部として。ただし、これは機能していません。************harplessa) 合計 40 時間のエントリのみを表示するにはどうすればよいですか。
SELECT RowType
,Person
,[Status]
,OrganizationalTeam
,Project
,Sprint
,Story
,[Task]
,MondayHours
,TuesdayHours
,WednesdayHours
,ThursdayHours
,FridayHours
,SaturdayHours
,SundayHours
,MAX(DerivedTable.TotalHours)
FROM (
SELECT '3' AS RowType
,DTH.PointPerson AS Person
,'' AS [Status]
,'' AS OrganizationalTeam
,'' AS Project
,'' AS Sprint
,'' AS Story
,'Subtotal:' AS Task
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 2
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS MondayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 3
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS TuesdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 4
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS WednesdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 5
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS ThursdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 6
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS FridayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 7
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS SaturdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 1
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS SundayHours
,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
FROM DailyTaskHours DTH
LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
LEFT JOIN ProductTeamUser PTU ON TSK.ProductTeamUserId = PTU.PK_ProductTeamUser
LEFT JOIN [User] USR ON PTU.UserId = USR.PK_User
LEFT JOIN OrganizationalTeam OLT ON USR.OrganizationalTeamId = OLT.PK_OrganizationalTeam
LEFT JOIN UserAuthorization UAN2 ON (
PDT.PK_Product = UAN2.ProductId
AND UAN2.Deleted IS NULL
)
WHERE DTH.PointPerson LIKE @userParam
AND ActivityDate >= @startDateParam
AND ActivityDate <= @endDateParam
AND COALESCE(OLT.PK_OrganizationalTeam, '') LIKE @orgTeamPK
AND UAN2.ProductId LIKE @productId
GROUP BY DTH.PointPerson
HAVING SUM(DTH.[Hours]) > 0
--
UNION
--
SELECT '3' AS RowType
,DTH.PointPerson AS Person
,'' AS [Status]
,'' AS OrganizationalTeam
,'' AS Project
,'' AS Sprint
,'' AS Story
,'Subtotal:' AS Task
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 2
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS MondayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 3
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS TuesdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 4
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS WednesdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 5
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS ThursdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 6
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS FridayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 7
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS SaturdayHours
,CAST(SUM(CASE
WHEN DATEPART(dw, DTH.ActivityDate) = 1
THEN DTH.[Hours]
ELSE 0
END) AS VARCHAR(20)) AS SundayHours
,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
FROM DailyTaskHours DTH
LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
LEFT JOIN ProductTeamUser PTU ON TSK.ProductTeamUserId = PTU.PK_ProductTeamUser
LEFT JOIN [User] USR ON PTU.UserId = USR.PK_User
LEFT JOIN OrganizationalTeam OLT ON USR.OrganizationalTeamId = OLT.PK_OrganizationalTeam
LEFT JOIN UserAuthorization UAN1 ON (
USR.PK_User = UAN1.UserId
AND UAN1.Deleted IS NULL
)
WHERE DTH.PointPerson LIKE @userParam
AND ActivityDate >= @startDateParam
AND ActivityDate <= @endDateParam
AND COALESCE(OLT.PK_OrganizationalTeam, '') LIKE @orgTeamPK
AND UAN1.OrganizationalTeamId LIKE @teamId
GROUP BY DTH.PointPerson
HAVING SUM(DTH.[Hours]) > 0
) AS DerivedTable
GROUP BY
DerivedTable.RowType,
DerivedTable.Person,
DerivedTable.[Status],
DerivedTable.OrganizationalTeam,
DerivedTable.Project,
DerivedTable.Sprint,
DerivedTable.Story,
DerivedTable.[Task],
DerivedTable.MondayHours,
DerivedTable.TuesdayHours,
DerivedTable.WednesdayHours,
DerivedTable.ThursdayHours,
DerivedTable.FridayHours,
DerivedTable.SaturdayHours,
DerivedTable.SundayHours,
DerivedTable.TotalHours