如何为复杂的MySQL查询添加另一列返回

时间:2013-06-24 16:36:51

标签: mysql

我有一个相当复杂的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列。我需要知道员工的午餐时间。我认为它将在选择部分中涉及子查询,因为它太复杂而无法以其他方式获得它。计算'工作时数'很复杂,因为我需要计算每天的(突破 - clockin)+(clockout - breakin)。现在我需要为每一天计算breakin - breakout。以下是一天员工当前表的结构。

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且未指定确切日期的情况下弄清楚如何使查询工作,那么请将其改为日期范围。

1 个答案:

答案 0 :(得分:1)

您应该将tpl.EmpID = 1588替换为tpl.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