2013-06-24 61 views
0

我有一個相當複雜的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列。我需要知道員工午餐的時間。我認爲這將涉及到select部分中的子查詢,因爲如果沒有其他方法,這將變得非常複雜。計算「工作時間」很複雜,因爲我需要計算每天的(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並沒有指定一個確切的日期,使之成爲日期範圍。

回答

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 
+0

這就是天才。使用該信息,我想出了我只需添加「DATE(tpl.PunchDateTime)= DATE(tp.PunchDateTime)」的WHERE條件以使日期具有動態性。謝謝! – dmikester1

+0

非常歡迎。 :-) – PerfectPixel