0

我正在处理一个很长的复杂查询:

DECLARE @userParam VARCHAR(100)
    ,@startDateParam DATETIME
    ,@endDateParam DATETIME
    ,@orgTeamPK VARCHAR(100)
    ,@teamId VARCHAR(100)
    ,@productId VARCHAR(100)

SET @userParam = '%'
SET @startDateParam = '2013-09-02 00:00:00'
SET @endDateParam = '2013-09-08 00:00:00'
SET @orgTeamPK = '%'
SET @teamId = '%'
SET @productId = '%'

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
) AS My_View
--

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

可以生成两行并将其放入行类型 3。它们之间有一个 UNION,因此将显示两个结果,但不会显示重复项。这按预期工作,但是在某些情况下,由于并非所有单元格在行之间加起来,它会显示该行的两个版本。如果发生这种情况,我只想显示最后一个单元格较大的行(总列)。

我如何实现这一目标?

编辑(试图澄清):

我有一个工会,有时会为同一个人产生两行:

如果发生这种情况,我只想显示总数较高的行,而不是两者。我怎么做? 在此处输入图像描述

编辑2:

更新了建议..仍然有相同的结果。怎么了?

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
4

1 回答 1

1

您如何将联合查询包装在外部查询中,然后使用聚合来获得更高的总列,如下所示(为简洁起见):

SELECT a.COL1, a.COL2, MAX(a.TOTAL) AS Total
FROM
(
   SELECT COL1, COL2, TOTAL 
   FROM TABLE1
   ...

   UNION ALL

   SELECT COL1, COL2, TOTAL 
   FROM TABLE2
   ...
) a

按 a.COL1、a.COL2 分组

您还可以使用其他聚合函数MIN/MAX用于您期望重复的其他列,您甚至可以将它们与CASE更复杂场景的语句混合使用。

于 2013-09-30T17:04:00.800 回答