Мне нужно вернуть общее время из таблицы с вычитанием времени перерыва на основе идентификатора смены.
CREATE FUNCTION FUNCTION_IDEL_TIME (@SHIFTID INT)
RETURNS INT
AS
BEGIN
DECLARE @IDEL_TIME INT
WITH CTE AS (
SELECT stb.StdBrkShiftId,
stb.ShiftId,
stb.ShiftParticulars [Break Particulars],
left(stb.FromTime, 8) FromTime,
left(stb.ToTime, 8) ToTime,
sum(cast(CONVERT(FLOAT, DATEADD(minute, DATEDIFF(minute, stb.FromTime, stb.ToTime), 0), 114) AS FLOAT) * 60 * 24) AS Minutes,
sum(cast(CONVERT(FLOAT, DATEADD(minute, DATEDIFF(minute, st.FromTime, st.ToTime), 0), 114) AS FLOAT) * 60 * 24) AS [Ideal available time]
FROM StdBreaksInShifts stb
INNER JOIN ShiftTimes st ON stb.ShiftId = st.ShiftId
WHERE stb.ShiftId = 1
GROUP BY stb.ShiftId,
ShiftParticulars,
stb.FromTime,
stb.ToTime,
stb.StdBrkShiftId
)
SELECT StdBrkShiftId,
ShiftId,
[Break Particulars],
FromTime,
ToTime,
Minutes,
[Ideal available time],
''
FROM CTE
GROUP BY ShiftId,
[Break Particulars],
FromTime,
ToTime,
Minutes,
[Ideal available time],
StdBrkShiftId
UNION ALL
SELECT StdBrkShiftId,
'' ShiftId,
'' [Break Particulars],
'' FromTime,
'' ToTime,
'' Minutes,
sum(Minutes) Total_Minutes,
@IDEL_TIME = ([Ideal available time] - SUM(Minutes))
FROM CTE
GROUP BY [Ideal available time]
RETURN @IDEL_TIME
END
ShiftId BreakP FromTime ToTime Diff Min Total_Mi Ideal avail
1 Morning Prayer 07:10:00 07:20:00 00:10 10 10 510
1 Morning Loading 07:20:00 07:35:00 00:15 15 15 510
1 Break for Hand wash 11:25:00 11:30:00 00:05 5 5 510
1 Lunch Break 11:30:00 12:00:00 00:30 30 30 510
1 Breaak for hand wash 14:15:00 14:20:00 00:05 5 5 510
1 Tea Break 14:20:00 14:30:00 00:10 10 10 510
1 Break for Machine 15:30:00 15:40:00 00:10 10 10 510
0 85 1.41 0
Кто-нибудь может подсказать, как написать функцию для этого?