0

私はクエリに取り組んでいます:

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 を数値に変換するときにエラーが発生しました。

エラーの場所がわかります。最初の 2 つのブロックでは、月曜日から金曜日までの列が 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() に変換しようとしています。

望ましい結果を得るにはどうすればよいですか?

4

2 に答える 2

1

これを試して:

    CAST (SUM(CASE 
              WHEN DATEPART(dw, DTH.ActivityDate) = 2
              THEN DTH.[Hours] 
              ELSE 0 END)
    AS VARCHAR(80)) AS Monday
于 2013-09-18T19:00:05.900 に答える