这是选择:
SELECT e.Id AS EmployeeId,
ISNULL(
SUM(CASE
WHEN DATEDIFF(MINUTE, o.StartDate, o.EndDate) < 360
THEN DATEDIFF(MINUTE, o.StartDate, o.EndDate)
WHEN DATEDIFF(MINUTE, o.StartDate, o.EndDate) < 560
THEN DATEDIFF(MINUTE, o.StartDate, o.EndDate) - 20
ELSE
DATEDIFF(MINUTE, o.StartDate, o.EndDate) - 45
END), 0) AS OvertimeSum
FROM
Overtimes o
RIGHT OUTER JOIN Employees e ON o.EmployeeId = e.Id
INNER JOIN Groups g ON g.Id = e.GroupId
WHERE
(((CONVERT(DATE, o.StartDate) >= @StartDate) AND
(CONVERT(DATE, o.StartDate) <= @EndDate)) OR
(o.StartDate IS NULL)) AND
(g.ShiftModelId = @ShiftModelId)
GROUP BY e.Id
我认为选择子句。它太长了,例如是否可以考虑DATEDIFF(MINUTE, o.StartDate, o.EndDate)
变量或其他东西。