0

我正在处理一个查询:

USE SCRUMAPI2

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

SET @userParam = 'David Tunnell (tunnelld)'
SET @startDateParam = '2013-10-21 00:00:00'
SET @endDateParam = '2013-10-27 23:59:59'
SET @orgTeamPK = '%'
SET @teamId = '%'
SET @productId = '%'
SET @search = '%%'

SELECT RowType AS RowType
    ,Person AS Person
    ,Project AS Project
    ,ProjectType AS ProjectType
    ,StoryNumber AS StoryNumber
    ,StoryTitle AS StoryTitle
    ,Effort AS Effort
    ,Task AS Task
    ,OriginalEstimateHours AS OriginalEstimateHours
    ,MondayHours AS Monday
    ,TuesdayHours AS Tuesday
    ,WednesdayHours AS Wednesday
    ,ThursdayHours AS Thursday
    ,FridayHours AS Friday
    ,SaturdayHours AS Saturday
    ,SundayHours AS Sunday
    ,TotalHours AS Total
FROM (
    -- DATE DISPLAY
    SELECT '1' AS RowType
        ,'' AS Person
        ,'' AS Project
        ,'Category' AS ProjectType
        ,'Incident #' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 0, @startDateParam)) = 2 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 0, @startDateParam), 111) ELSE '' END) AS MondayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 1, @startDateParam)) = 3 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 1, @startDateParam), 111) ELSE '' END) AS TuesdayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 2, @startDateParam)) = 4 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 2, @startDateParam), 111) ELSE '' END) AS WednesdayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 3, @startDateParam)) = 5 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 3, @startDateParam), 111) ELSE '' END) AS ThursdayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 4, @startDateParam)) = 6 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 4, @startDateParam), 111) ELSE '' END) AS FridayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 5, @startDateParam)) = 7 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 5, @startDateParam), 111) ELSE '' END) AS SaturdayHours
        ,(CASE WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7 THEN '' WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5 THEN '' WHEN DATEPART(dw, DATEADD(DAY, 6, @startDateParam)) = 1 THEN CONVERT(VARCHAR(10), DATEADD(DAY, 6, @startDateParam), 111) ELSE '' END) AS SundayHours
        ,'' AS TotalHours
    --

    UNION ALL

    --
    -- GRAND TOTALS
    --
    SELECT '2' AS RowType
        ,'All Personnel' AS Person
        ,'' AS Project
        ,'' AS ProjectType
        ,'' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'Total:' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 2 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Monday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 3 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Tuesday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 4 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Wednesday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 5 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Thursday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 6 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Friday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 7 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Saturday
        ,CAST(SUM(CASE WHEN DATEPART(dw, DTH.ActivityDate) = 1 THEN DTH.[Hours] ELSE 0 END) AS VARCHAR(20)) AS Sunday
        ,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS Total
    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 UAN ON (
            PDT.PK_Product = UAN.ProductId
            AND USR.OrganizationalTeamId = UAN.OrganizationalTeamId
            AND UAN.Deleted IS NULL
            )
    WHERE DTH.PointPerson LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND COALESCE(OLT.PK_OrganizationalTeam, '') LIKE @orgTeamPK
        AND (
            COALESCE(UAN.ProductId, '') LIKE @productId
            OR COALESCE(UAN.OrganizationalTeamId, '') LIKE @teamId
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR STY.Number IS NULL
                )
            )

    UNION ALL

    --
    -- Details by PERSON, PROJECT, SPRINT, STORY, TASK
    --
    SELECT '3' AS RowType
        ,DTH.PointPerson AS Person
        ,COALESCE(PDT.[Name], APP.AppName) 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' ELSE SCY.Catagory END
            ) AS ProjectType
        ,COALESCE(STY.[Number], NSS.IncidentNumber) AS StoryNumber
        ,COALESCE(STY.Title, NSS.[Description]) AS StoryTitle
        ,STY.Effort AS Effort
        ,COALESCE(TSK.[Name], '') AS Task
        ,TSK.OriginalEstimateHours AS OriginalEstimateHours
        ,SCY.Catagory AS Category
        ,NSS.IncidentNumber AS IncidentNumber
        ,APP.AppName AS ApplicationName
        ,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 SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
    LEFT JOIN [Application] APP ON NSS.ApplicationId = APP.PK_Application
    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 UAN ON (
            PDT.PK_Product = UAN.ProductId
            AND USR.OrganizationalTeamId = UAN.OrganizationalTeamId
            AND UAN.Deleted IS NULL
            )
    WHERE DTH.PointPerson LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND COALESCE(OLT.PK_OrganizationalTeam, '') LIKE @orgTeamPK
        AND (
            COALESCE(UAN.ProductId, '') LIKE @productId
            OR COALESCE(UAN.OrganizationalTeamId, '') LIKE @teamId
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR STY.Number IS NULL
                )
            )
    GROUP BY DTH.PointPerson
        ,PDT.[Name]
        ,SPT.[Name]
        ,SPT.[Description]
        ,STY.[Number]
        ,STY.Title
        ,TSK.[Name]
        ,SCY.Catagory
        ,NSS.IncidentNumber
        ,APP.AppName
        ,STY.KanBanProductId
        ,STY.SprintId
        ,NSS.[Description]
        ,TSK.OriginalEstimateHours
        ,STY.Effort
    HAVING SUM(DTH.[Hours]) > 0
    --

    UNION ALL

    --
    -- Sub-TOTAL by PERSON
    --
    SELECT '4' AS RowType
        ,DTH.PointPerson AS Person
        ,'' AS Project
        ,'' AS ProjectType
        ,'' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'Subtotal:' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,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 SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
    LEFT JOIN [Application] APP ON NSS.ApplicationId = APP.PK_Application
    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 UAN ON (
            PDT.PK_Product = UAN.ProductId
            AND USR.OrganizationalTeamId = UAN.OrganizationalTeamId
            AND UAN.Deleted IS NULL
            )
    WHERE DTH.PointPerson LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND COALESCE(OLT.PK_OrganizationalTeam, '') LIKE @orgTeamPK
        AND (
            COALESCE(UAN.ProductId, '') LIKE @productId
            OR COALESCE(UAN.OrganizationalTeamId, '') LIKE @teamId
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR STY.Number IS NULL
                )
            )
    GROUP BY DTH.PointPerson
    HAVING SUM(DTH.[Hours]) > 0
    ) AS My_View
ORDER BY Person
    ,RowType
    ,Project
    ,ProjectType
    ,StoryNumber
    ,StoryTitle
    ,Task

这就是返回的内容: 在此处输入图像描述

我将 Effort 和 OriginalEstimateHours 设置为空,但返回 0。如何使单元格为空白?

4

3 回答 3

2

它们的类型是 INT 和 BIGINT,

如果您想要一个空字符串,而不是0or NULL,您将需要使用CASTorCONVERT函数将列转换为 varchar 类型。您不能在数字列中放置空字符串。然后,您可能还需要使用CASE语句将0字符串转换为空字符串。

于 2013-11-01T14:33:14.190 回答
1

我怀疑STY.Effortand TSK.OriginalEstimateHours(在3SELECT行的子句中RowType)是数字的。

从文档中UNION

作为 UNION 操作一部分的列的定义不必相同,但它们必须通过隐式转换兼容。当数据类型不同时,根据数据类型优先规则确定生成的数据类型。

我的重点

将这些列包装起来,CONVERT(varchar(20),STY.Effort)以便为这些列选择的类型varchar而不是数字类型。

于 2013-11-01T14:35:57.637 回答
1

当您分配空字符串时,取决于数据类型,它被转换为不同的值。0 表示整数,1900-01-01 表示日期等。有关更多信息,请参阅此 http://beyondrelational.com/modules/2/blogs/70/posts/10841/empty-string-and-default-values.aspx

于 2013-11-01T14:41:59.807 回答