0

我试图加入我的查询,但我被困在周日时间,

我有一个非常基本且直接的联接查询,用于计算员工的所有工作时间、正常时间和加班时间,并且查询运行良好,但是当我想加入星期天时间时,一切都变得混乱了......

没有周日时间的完整查询:-

SELECT
`empl_attendance`.`employee_id`,
`employee`.`employee_surname`,
`employee`.`employee_first_name`,
`employee`.`employee_second_name`,
FORMAT((IF(SUM...some basic maths...)),1) AS nt,
FORMAT((...some basic maths...),1) AS ot,
FORMAT((...some basic maths...),1) AS total
FROM
`empl_attendance`
WHERE
 (`empl_attendance_date` BETWEEN '$start_date' AND '$end_date')
INNER JOIN 
`employee` 
ON `empl_attendance`.`employee_id`=`employee`.`employee_id` 
GROUP BY `employee_id`

但是我需要结合下面的查询,它基本上计算了每个员工的周日总小时数。该查询本身可以正常工作,我只是在努力结合这两个查询:

SELECT SUM(`empl_attendance_total`) AS sundays
FROM `empl_attendance`
WHERE
(`empl_attendance_date` BETWEEN '$start_date' AND '$end_date')
AND
WEEKDAY(`empl_attendance_date`) > 5
GROUP BY `employee_id`

谢谢

周杰伦

4

1 回答 1

0

您可以使用 case 语句来做到这一点:

 SELECT  ea.`employee_id`, e.`employee_surname`,  e.`employee_first_name`,  e.`employee_second_name`,
         FORMAT((IF(SUM...some basic maths...)),1) AS nt,
         FORMAT((...some basic maths...),1) AS ot,
         FORMAT((...some basic maths...),1) AS total,
         sum(case when weekday(ea.empl_attendance_date) > 5 then ea.empl_attendance_total end) as SundayTotal
 FROM empl_attendance ea join
      employee e
      ON ea.`employee_id = e.employee_id
  WHERE  ea.empl_attendance_date BETWEEN '$start_date' AND '$end_date'
  GROUP BY `employee_id`  

我假设以 FORMAT 开头的行正在寻找非星期天的日期,或者至少包括它们,如果这是你想要的。

于 2012-08-28T14:52:17.743 回答