0

我有一个相当复杂的 MySQL 查询,如下所示:

SELECT CONCAT(pe.first, ' ', pe.last) AS Name,
                   tp.EmpID as 'Empl ID',
                   DATE_FORMAT(tp.PunchDateTime, '%m-%d-%Y') AS 'Punch Date',
                   DATE_FORMAT(tp.PunchDateTime, '%W') AS 'Weekday',
                   TRUNCATE((SUM(UNIX_TIMESTAMP(PunchDateTime) * (1 - 2 * `In-Out`)) / 3600),2)
                      AS 'Hours Worked'
              FROM timeclock_punchlog tp LEFT JOIN prempl01 pe ON tp.EmpID = pe.prempl
             WHERE tp.PunchDateTime >= '2013-06-16' and tp.PunchDateTime < '2013-06-23'
             AND tp.EmpID = 1588
            GROUP BY date(PunchDateTime), EmpID
            ORDER BY Name, `Punch Date` ASC

现在我需要添加第 6 列。我需要知道员工的午餐时间。我认为这将涉及选择部分中的子查询,因为以其他方式获取它太复杂了。计算“工作时间”很复杂,因为我需要计算每天的 (breakout - clockin) + (clockout - breakin)。现在我还需要计算每一天的突破-突破。这是一位员工一天的当前表的结构。

PunchID EmpID   PunchEvent    PunchDateTime           In-Out
308     1588    clockin       6/17/2013 6:20:48 AM    Checked
313     1588    breakout      6/17/2013 12:15:18 PM   Unchecked
315     1588    breakin       6/17/2013 12:43:58 PM   Checked
319     1588    clockout      6/17/2013 5:00:37 PM    Unchecked

我不知道如何将午休时间添加到上述查询中。希望我已经提供了所有需要的信息。

更新:我已经整理了一个工作查询,可以为特定日期和特定员工执行我想要的操作。现在我需要的是让这个查询适用于特定日期范围(即周)的所有员工。这是查询:

SELECT CONCAT(pe.first, ' ', pe.last) AS Name,
   tp.EmpID AS 'Empl ID',
   DATE_FORMAT(tp.PunchDateTime, '%m-%d-%Y') AS 'Punch Date',
   DATE_FORMAT(tp.PunchDateTime, '%W') AS 'Weekday',
   TRUNCATE((SUM(UNIX_TIMESTAMP(PunchDateTime) * (1 - 2 * `In-Out`)) / 3600), 2)
      AS 'Hours Worked',
   (SELECT TIMEDIFF
 ((SELECT DATE_FORMAT(tpl.PunchDateTime, '%r') as dTime FROM timeclock_punchlog tpl WHERE tpl.PunchEvent = 'breakin' AND tpl.EmpID = 1588 AND DATE(tpl.PunchDateTime) = '2013-06-17'),
(SELECT DATE_FORMAT(tpl.PunchDateTime, '%r') as dTime FROM timeclock_punchlog tpl WHERE tpl.PunchEvent = 'breakout' AND tpl.EmpID = 1588 AND DATE(tpl.PunchDateTime) = '2013-06-17'))) as 'Lunch'
                      FROM timeclock_punchlog tp LEFT JOIN prempl01 pe ON tp.EmpID = pe.prempl
                     WHERE DATE(tp.PunchDateTime) = '2013-06-17'
                     AND tp.EmpID = 1588
                    GROUP BY date(PunchDateTime), EmpID
                    ORDER BY Name, `Punch Date` ASC

结果:

Name            Empl ID Punch Date  Weekday Hours Worked    Lunch
BRUCE COLEMAN   1588    06-17-2013  Monday  10.18           00:28:40

现在,如果有人可以在不指定员工 ID 和不指定确切日期的情况下弄清楚如何使该查询工作,则将其设为日期范围。

4

1 回答 1

1

您应该将两者都替换tpl.EmpID = 1588tpl.EmpID = tp.EmpID并删除AND tp.EmpID = 1588.

编辑:

SELECT CONCAT(pe.first, ' ', pe.last) AS Name,
       tp.EmpID AS 'Empl ID',
       DATE_FORMAT(tp.PunchDateTime, '%m-%d-%Y') AS 'Punch Date',
       DATE_FORMAT(tp.PunchDateTime, '%W') AS 'Weekday',
       TRUNCATE((SUM(UNIX_TIMESTAMP(PunchDateTime) * (1 - 2 * `In-Out`)) / 3600), 2) AS 'Hours Worked',
       (SELECT TIMEDIFF((SELECT DATE_FORMAT(tpl.PunchDateTime, '%r') as dTime FROM timeclock_punchlog tpl WHERE tpl.PunchEvent = 'breakin' AND tpl.EmpID = tp.EmpID AND DATE(tpl.PunchDateTime) = '2013-06-17'),
                        (SELECT DATE_FORMAT(tpl.PunchDateTime, '%r') as dTime FROM timeclock_punchlog tpl WHERE tpl.PunchEvent = 'breakout' AND tpl.EmpID = tp.EmpID AND DATE(tpl.PunchDateTime) = '2013-06-17'))) as 'Lunch'
FROM timeclock_punchlog tp LEFT JOIN prempl01 pe ON tp.EmpID = pe.prempl
WHERE DATE(tp.PunchDateTime) = '2013-06-17'
GROUP BY date(PunchDateTime), EmpID
ORDER BY Name, `Punch Date` ASC
于 2013-06-24T18:11:03.470 回答