我一直在处理一个查询:
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()
我怎样才能达到预期的效果?