3

我有一个现有的查询,它会为每个员工每天提供总小时数。我需要获取每个员工在两个给定日期之间的总小时数。所以基本上我需要总结每个员工的小时数列。这是我现有的查询,每天提供总计:

SELECT CONCAT(pe.first, ' ', pe.last) AS Name,
           tp.EmpID AS 'Empl ID',
           DATE_FORMAT(tp.PunchDateTime, '%m-%d-%Y') AS 'Punch Date',
           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-5-26'
           AND tp.PunchDateTime < '2013-6-1'
    GROUP BY date(PunchDateTime), EmpID
    ORDER BY EmpID ASC

它给出这样的输出:

Name                Empl ID Punch Date  Hours Worked
TERESA A. EUBANKS   0354    05-29-2013      0.00
TRACY D CURTIS      0364    05-28-2013      6.15
TRACY D CURTIS      0364    05-29-2013      0.00
KENT KADAVY         0452    05-31-2013      2.38
JANE M SMITH        0456    05-31-2013      0.02
JANE M SMITH        0456    05-29-2013      0.01
JANE M SMITH        0456    05-28-2013      3.15
CINDY JEAN ARREY    0458    05-28-2013      8.16

基本上,我想要一个查询,它将获取上面的输出并显示每个员工的工作时间总和。我不需要显示每天的工作时间。

谢谢!麦克风

4

1 回答 1

0

我让它变得复杂。不需要子查询。这是我的最终查询,以产生我想要的结果。

    SELECT CONCAT(pe.first, ' ', pe.last) AS Name,
       tp.EmpID AS 'Empl ID',
       DATE_FORMAT(tp.PunchDateTime, '%m-%d-%Y') AS 'Punch Date',
       TRUNCATE(
          (SUM(UNIX_TIMESTAMP(PunchDateTime) * (1 - 2 * `In-Out`)) / 3600),
          3)
          AS 'Hours Worked'
  FROM timeclock_punchlog tp LEFT JOIN prempl01 pe ON tp.EmpID = pe.prempl
 WHERE tp.PunchDateTime >= '2013-5-1' AND tp.PunchDateTime < '2013-5-28'
GROUP BY tp.EmpID;
于 2013-06-04T18:42:37.743 回答