简体   繁体   English

如何使用mysql从子查询中总计总计

[英]How to sum totals from a subquery using mysql

I have an existing query that spits out total hours for each employee for each day. 我有一个现有的查询,每天为每位员工计算总小时数。 I need to get total hours between the two given dates for each employee. 我需要在每个员工的两个给定日期之间获得总时数。 So basically I need to sum up the hours column for each employee. 所以基本上我需要总结每个员工的小时栏。 Here is my existing query that gives totals per day: 这是我现有的查询,每天给出总数:

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

And it gives output like this: 它提供如下输出:

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

Basically I want a query that will take that output above and show the sum of the hours worked for each employee. 基本上我想要一个查询,它将获取上面的输出并显示每个员工工作小时数的总和。 I don't need to show the hours worked per day. 我不需要显示每天的工作时间。

Thanks! 谢谢! Mike 麦克风

I was making it way to complicated. 我正在努力复杂化。 No subquery needed. 不需要子查询。 Here is my final query to produce my desired results. 这是我最终的查询,以产生我想要的结果。

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM