在UNION期间CASTing导致错误

时间:2013-09-18 18:49:56

标签: sql sql-server

我一直致力于查询:

DECLARE @userParam VARCHAR(100)
    ,@startDateParam DATETIME

SET @userParam = ''
SET @startDateParam = '2013-09-02 00:00:00'

SELECT '1' AS RowType
    ,'Person' AS Person
    ,'Project' AS Project
    ,'Sprint' AS Sprint
    ,'Story' AS Story
    ,'Task' AS Task
    ,(
        CASE 
            WHEN DATEPART(dw, @startDateParam) = 2
                THEN 'Monday'
            ELSE ''
            END
        ) AS Monday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 1, @startDateParam)) = 3
                THEN 'Tuesday'
            ELSE ''
            END
        ) AS Tuesday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 2, @startDateParam)) = 4
                THEN 'Wednesday'
            ELSE ''
            END
        ) AS Wednesday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 3, @startDateParam)) = 5
                THEN 'Thursday'
            ELSE ''
            END
        ) AS Thursday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 4, @startDateParam)) = 6
                THEN 'Friday'
            ELSE ''
            END
        ) AS Friday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 5, @startDateParam)) = 7
                THEN 'Saturday'
            ELSE ''
            END
        ) AS Saturday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 6, @startDateParam)) = 1
                THEN 'Sunday'
            ELSE ''
            END
        ) AS Sunday
    ,'Total' AS Total
--

UNION ALL

--
SELECT '2' AS RowType
    ,'' AS Person
    ,'' AS Project
    ,'' AS Sprint
    ,'' AS Story
    ,'' AS Task
    ,(
        CASE 
            WHEN DATEPART(dw, @startDateParam) = 2
                THEN CONVERT(VARCHAR(10), @startDateParam, 111)
            ELSE ''
            END
        ) AS Monday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 1, @startDateParam)) = 3
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 1, @startDateParam), 111)
            ELSE ''
            END
        ) AS Tuesday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 2, @startDateParam)) = 4
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 2, @startDateParam), 111)
            ELSE ''
            END
        ) AS Wednesday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 3, @startDateParam)) = 5
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 3, @startDateParam), 111)
            ELSE ''
            END
        ) AS Thursday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 4, @startDateParam)) = 6
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 4, @startDateParam), 111)
            ELSE ''
            END
        ) AS Friday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 5, @startDateParam)) = 7
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 5, @startDateParam), 111)
            ELSE ''
            END
        ) AS Saturday
    ,(
        CASE 
            WHEN DATEPART(dw, DATEADD(DAY, 6, @startDateParam)) = 1
                THEN CONVERT(VARCHAR(10), DATEADD(DAY, 6, @startDateParam), 111)
            ELSE ''
            END
        ) AS Sunday
    ,'' AS Total
--

UNION ALL

--
SELECT '3' AS RowType
    ,DTH.PointPerson AS Person
    ,PDT.[Name] 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], NSS.IncidentNumber) AS Story
    ,TSK.[Name] AS Task
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 2
                THEN DTH.[Hours]
            ELSE 0
            END) AS Monday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 3
                THEN DTH.[Hours]
            ELSE 0
            END) AS Tuesday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 4
                THEN DTH.[Hours]
            ELSE 0
            END) AS Wednesday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 5
                THEN DTH.[Hours]
            ELSE 0
            END) AS Thursday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 6
                THEN DTH.[Hours]
            ELSE 0
            END) AS Friday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 7
                THEN DTH.[Hours]
            ELSE 0
            END) AS Saturday
    ,SUM(CASE 
            WHEN DATEPART(dw, DTH.ActivityDate) = 1
                THEN DTH.[Hours]
            ELSE 0
            END) AS Sunday
    ,SUM(DTH.[Hours]) 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
GROUP BY DTH.PointPerson
    ,PDT.[Name]
    ,SPT.[Name]
    ,SPT.[Description]
    ,STY.[Number]
    ,NSS.IncidentNumber
    ,TSK.[Name]
HAVING SUM(DTH.[Hours]) > 0

第2和第3块之间的UNION导致问题。

具体来说:将数据类型varchar转换为数字时出错。

我看到错误的位置,在前两个块中,星期一到星期五的列是varchar,而在最后一个块中它们不是。

我尝试过施展它们:

SUM(CASE 
        WHEN DATEPART(dw, DTH.ActivityDate) = 2
            THEN CAST (DTH.[Hours] AS VARCHAR(80))
        ELSE 0
        END) AS Monday

我现在收到此错误:将varchar值'0.25'转换为数据类型int时转换失败。

我不明白为什么,据我所知,我不是想将0.25转换为int我试图将其转换为varchar()

如何达到预期效果?

2 个答案:

答案 0 :(得分:2)

CASE中的ELSE部分返回一个数值,该值导致返回值的数值数据类型。您必须将CAST移到SUM之外:

CAST(SUM(CASE 
        WHEN DATEPART(dw, DTH.ActivityDate) = 2
            THEN DTH.[Hours]
        ELSE 0
        END) AS VARCHAR(80)) AS Monday

答案 1 :(得分:1)

试试这个:

    CAST (SUM(CASE 
              WHEN DATEPART(dw, DTH.ActivityDate) = 2
              THEN DTH.[Hours] 
              ELSE 0 END)
    AS VARCHAR(80)) AS Monday