1

我正在处理一个 MySQL 查询以返回一些特定的结果。我非常接近。现在,我需要计算两列之间的 TIMEDIFF。这是我当前的查询:

SELECT DATE_FORMAT(PunchDateTime, '%W') DAY, MAX(CASE WHEN PunchEvent = 'ClockIn' THEN DATE_FORMAT(PunchDateTime, '%r') END) AS 'Clock In', MAX(CASE WHEN PunchEvent = 'BreakOut' THEN 
    DATE_FORMAT(PunchDateTime, '%r') END) AS 'Break Out', MAX(CASE WHEN PunchEvent = 'BreakIn' THEN DATE_FORMAT(PunchDateTime, '%r') END) 'Break In', MAX(CASE WHEN PunchEvent = 'ClockOut' THEN 
    DATE_FORMAT(PunchDateTime, '%r') END) 'Clock Out', TIMEDIFF('Clock In', 'Break Out') AS Total FROM timeclock_punchlog WHERE EmpID = 456 GROUP BY 
    DATE_FORMAT(PunchDateTime, '%W') ORDER BY PunchDateTime;

现在,Total 显示 00:00:00。任何想法我怎么能做到这一点?谢谢迈克

4

1 回答 1

0

经过大量的谷歌搜索和修补,我想通了。我发现您不能在 TIMEDIFF 函数的查询中使用之前创建的别名。我基本上不得不使用我用来创建别名的整个公式来代替 TIMEDIFF 函数中的别名。这是我的查询:

SELECT  DATE_FORMAT(PunchDateTime, '%W') DAY, MAX(CASE WHEN PunchEvent = 'ClockIn' THEN DATE_FORMAT(PunchDateTime, '%T') END) AS 'Clock In', MAX(CASE WHEN PunchEvent = 'BreakOut' THEN DATE_FORMAT(PunchDateTime, '%T') END) AS 'Break Out', MAX(CASE WHEN PunchEvent = 'BreakIn' THEN DATE_FORMAT(PunchDateTime, '%T') END) 'Break In', MAX(CASE WHEN PunchEvent = 'ClockOut' THEN DATE_FORMAT(PunchDateTime, '%T') END) 'Clock Out', (TIMEDIFF(MAX(CASE WHEN PunchEvent = 'clockout' THEN DATE_FORMAT(PunchDateTime, '%T') END), MAX(CASE WHEN PunchEvent = 'breakin' THEN DATE_FORMAT(PunchDateTime, '%T') END))) AS Total FROM timeclock_punchlog WHERE EmpID = 456 GROUP BY DATE_FORMAT(PunchDateTime, '%W') ORDER BY PunchDateTime;

我还必须将我的 %r 更改为 %T,这样它才能根据军事时间而不是标准的 AM/PM 时间正确计算 TIMEDIFF。希望这对某人有帮助!麦克风

于 2013-05-16T15:28:09.887 回答