请帮助并非常感谢你!
我如何得到这个结果?
http://i.stack.imgur.com/EedXW.png
这是我目前的结果:
http://i.stack.imgur.com/Ydzn0.png
代码:
WITH shiftHours AS
(
SELECT
RowID,
y.EMPLOYEENAME AS EMPLOYEENAME,
-- flatten the first hour to remove the minutes and get the initial current hour
DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) AS currentHour,
ShiftA_Start,
ShiftA_End,
DATEPART(hour, ShiftA_Start) AS hourOrdinal,
-- determine how much of the first hour is applicable. if it is minute 0 then the whole hour counts
CAST(CASE
WHEN DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) = DATEADD(hour, DATEDIFF(hour, 0, ShiftA_End), 0) THEN DATEDIFF(minute, ShiftA_Start, ShiftA_End) / 60.0
WHEN DATEPART(minute, ShiftA_Start) = 0 THEN 1.0
ELSE (60 - DATEPART(minute, ShiftA_Start)) / 60.0
END AS DECIMAL(5,3)) AS hourValue
FROM
(-- use a ROW_NUMBER() to generate row IDs for the shifts to ensure each row is unique once it gets to the pivot
SELECT
ROW_NUMBER() OVER(ORDER BY ShiftA_Start, ShiftA_End) AS RowID,
EMPLOYEENAME,
ShiftA_Start,
ShiftA_End
FROM
(-- this is where the data gets pulled from the source table and where the data types are converted from string to DATETIME
SELECT
EMPLOYEENAME,
CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_Start,
CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_end,
CAST(CASE
WHEN DATEPART (day, [ShiftA_Start]) = DATEPART (day, [SHIFTA_END])
THEN CONVERT(DATETIME, LEFT(SHIFTA_end, 17), 103)
WHEN DATEPART (hour, [ShiftA_Start]) = DATEPART (hour, [SHIFTA_END])
THEN CONVERT(DATETIME, LEFT(SHIFTA_end, 17), 103) + '23:59:00.000' END AS VARCHAR(30)) AS S_END
FROM
[DatabaseName].[dbo].[TMS_PEOPLE]
WHERE
CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) IS NOT NULL
AND CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) IS NOT NULL
AND CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) != CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103)
AND CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) != '1900-01-01 00:00:00.000'
AND CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) != '1900-01-01 00:00:00.000'
--AND CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) = '2016-01-24 14:09:00.000'
AND EMPLOYEENAME = 'MUHAMMAD BIN PARMIN'
-- this is also where you would add any filtering from the source table such as date ranges
) x
) AS y
UNION ALL
SELECT RowID,
EMPLOYEENAME,
-- add an hour to the currentHour each time the recursive CTE is called
DATEADD(hour, 1, currentHour) AS currentHour,
ShiftA_Start,
ShiftA_End,
DATEPART(hour, DATEADD(hour, 1, currentHour)) AS hourOrdinal,
CAST(CASE
-- when this is the last time period determine the amount of the hour that is applicable
WHEN DATEADD(hour, 2, currentHour) > ShiftA_End THEN DATEPART(minute, ShiftA_End) / 60.0
ELSE 1
END AS DECIMAL(5,3)) AS hourValue
from shiftHours
-- contine recursion until the next hour is after the ShiftEnd
WHERE DATEADD(hour, 1, currentHour) < ShiftA_End
)
SELECT *
FROM (
SELECT RowID,
EMPLOYEENAME,
ShiftA_Start,
ShiftA_End,
hourValue,
hourOrdinal
from shiftHours
) AS t
PIVOT (
SUM(hourValue)
FOR hourOrdinal IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23])
) AS pvt
OPTION (MAXRECURSION 0);
JKQDJKBDJAJSDudkbsjkdbjkBhsjkdbnbjasadbjbdjajdbka dhajdkbobKJBDJBAJBDO
答案 0 :(得分:1)
在你的内部查询中,你有这一行
CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_end,
你不应该使用班次结束吗?