以下是我的SQL查询:
select Employee_Number, Cast([DateTime] as Date) as 'DateTime', MIN([DateTime]) as 'MIN', MAX([DateTime]) as 'MAX' , [Hours Worked] =
CAST((DATEDIFF(HOUR ,min([DateTime]), max([DateTime])) / 24) AS VARCHAR) +
' Days ' +
CAST((DATEDIFF(HOUR , min([DateTime]), max([DateTime])) % 24) AS VARCHAR) +
' Hours ' +
CAST((DATEDIFF(MINUTE, min([DateTime]), max([DateTime])) % 60) AS VARCHAR) +
' Minutes ' +
CAST((DATEDIFF(SECOND, min([DateTime]), max([DateTime])) % 60) AS VARCHAR) +
' Seconds ' , [Over Time] = CAST((DATEDIFF(HOUR, '17:30:00:000', MAX([DateTime])) % 24) AS VARCHAR) + ' Hours ' +
CAST((DATEDIFF(MINUTE, '17:30:00:000', max([DateTime])) % 60) AS VARCHAR) + ' Minutes', [LATE] =
CAST((DATEDIFF(HOUR, '08:30:00:000',Min([DATETIME])) % 24) AS VARCHAR) + ' Hours ' + CAST((DATEDIFF(MINUTE, '08:30:00:000', MIN([DateTime])) % 60) AS VARCHAR) + ' Minutes'
from tblExtract group by Employee_Number, Cast([DateTime] as Date)
这就是此查询显示的内容:
如果员工在8:30之前提前入境,并且如果他在17:30之前提前离开,则会出现问题
我该如何纠正?我如何得到一个案例,如果员工离开或提前到达LATE或加班专栏的价值为'0小时0分钟'?
答案 0 :(得分:2)
如果您希望在某人到达/提前离开时显示'0 Hours 0 Minutes'
,则可以使用CASE
。
select Employee_Number, Cast([DateTime] as Date) as 'DateTime', MIN([DateTime]) as 'MIN', MAX([DateTime]) as 'MAX' , [Hours Worked] =
CAST((DATEDIFF(HOUR ,min([DateTime]), max([DateTime])) / 24) AS VARCHAR) +
' Days ' +
CAST((DATEDIFF(HOUR , min([DateTime]), max([DateTime])) % 24) AS VARCHAR) +
' Hours ' +
CAST((DATEDIFF(MINUTE, min([DateTime]), max([DateTime])) % 60) AS VARCHAR) +
' Minutes ' +
CAST((DATEDIFF(SECOND, min([DateTime]), max([DateTime])) % 60) AS VARCHAR) +
' Seconds ' , [Over Time] = CASE WHEN DATEDIFF(MINUTE, '17:30:00:000', CONVERT(TIME, MAX([DateTime]))) < 0
THEN '0 Hours 0 Minutes'
ELSE
CAST((DATEDIFF(HOUR, '17:30:00:000', MAX([DateTime])) % 24) AS VARCHAR) + ' Hours ' +
CAST((DATEDIFF(MINUTE, '17:30:00:000', max([DateTime])) % 60) AS VARCHAR) + ' Minutes'
END,
[LATE] = CASE WHEN DATEDIFF(MINUTE, '08:30:00:000', CONVERT(TIME, MIN([DateTime]))) < 0
THEN '0 Hours 0 Minutes'
ELSE
CAST((DATEDIFF(HOUR, '08:30:00:000',Min([DATETIME])) % 24) AS VARCHAR) +
' Hours ' + CAST((DATEDIFF(MINUTE, '08:30:00:000', MIN([DateTime])) % 60) AS VARCHAR) + ' Minutes'
END
from Extraction
group by Employee_Number, Cast([DateTime] as Date)