我正在处理一个查询,并且正在获得我不期望的结果。这是查询:
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,我该如何确保它?
答案 0 :(得分:1)
您的OrganizationalTeam专栏通过DailyTaskHours加入&gt;任务&gt; ProductTeamUser&gt;用户。每个连接都可以生成NULL值,因此请检查每个连接。尝试使用SELECT *
运行该查询并使用WHERE
仅返回这两行中的一行。