0

我正在处理一个查询并且得到了我不期望的结果。这是查询:

USE SCRUMAPI2

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

SET @userParam = '%'
SET @startDateParam = '2013-10-14 00:00:00'
SET @endDateParam = '2013-10-20 23:59:59'
SET @orgTeamPK = '%'
SET @teamId = '%'
SET @productId = '%'

    SELECT '3' AS RowType
        ,DTH.PointPerson AS Person
        ,(
            CASE 
                WHEN USR.[Deleted] IS NULL
                    THEN 'Active'
                ELSE 'Inactive'
                END
            ) AS [Status]
        ,USR.OrganizationalTeamId AS OrganizationalTeam
        ,COALESCE(PDT.[Name], APP.AppName) AS Project
        ,(
            CASE 
                WHEN TSK.NAME IS NULL
                    THEN NULL
                WHEN SPT.[Name] + ' - ' + SPT.[Description] IS NULL
                    THEN 'KanBan'
                ELSE SPT.[Name] + ' - ' + SPT.[Description]
                END
            ) AS Sprint
        ,COALESCE(STY.[Number], SCY.Catagory) AS Story
        ,COALESCE(TSK.[Name], NSS.IncidentNumber) 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 SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory 
    LEFT JOIN [Application] APP ON NSS.ApplicationId = APP.PK_Application 
    LEFT JOIN UserAuthorization UAN1 ON (
            PDT.PK_Product = UAN1.ProductId
            AND UAN1.Deleted IS NULL
            )
    LEFT JOIN UserAuthorization UAN2 ON (
            OLT.PK_OrganizationalTeam = UAN2.OrganizationalTeamId
            AND UAN2.Deleted IS NULL
            )
    WHERE DTH.PointPerson LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND COALESCE(USR.OrganizationalTeamId, '') LIKE @orgTeamPK
        AND (
            COALESCE(UAN1.ProductId, '') LIKE @productId
            OR COALESCE(UAN2.OrganizationalTeamId, '') LIKE @teamId
            )
    GROUP BY DTH.PointPerson
        ,PDT.[Name]
        ,SPT.[Name]
        ,SPT.[Description]
        ,STY.[Number]
        ,NSS.IncidentNumber
        ,TSK.[Name]
        ,OLT.[Name]
        ,USR.[Deleted]
        ,APP.AppName
        ,SCY.Catagory
        ,NSS.IncidentNumber
        ,USR.OrganizationalTeamId
    HAVING SUM(DTH.[Hours]) > 0

这是结果集的一部分:

在此处输入图像描述

对于第一个用户的结果,OrganizationTeams 列中的两个单元格返回 NULL。我不明白为什么会这样,这个用户被分配了 3 的组织团队:

在此处输入图像描述

为什么它不为该用户的所有单元格返回 3,我如何确保它返回?

4

1 回答 1

1

您的 OrganizationalTeam 列通过 DailyTaskHours > Task > ProductTeamUser > User 加入。每个连接都可能导致 NULL 值,因此请检查每个连接。尝试使用SELECT *并使用 a运行该查询以WHERE仅返回这两行之一。

于 2013-10-17T15:25:35.510 回答