I have a table of users and a table of time-entries. I am trying to obtain the sum(time_entry.hours_worked)
per employee where the date is within a range of values.
With:
SELECT employee.id, COALESCE(SUM(time_entry.hours_worked),0) as `sum`
FROM employee
LEFT JOIN time_entry
ON employee.id = time_entry.student_id
GROUP BY employee.id;
I am able to obtain entries for all employees, even if no hours are worked :
+----+--------+
| id | sum |
+----+--------+
| 1 | 191.00 |
| 2 | 48.00 |
| 3 | 0.00 |
+----+--------+
With a where statement:
SELECT employee.id, COALESCE(SUM(time_entry.hours_worked),0) AS `sum`
FROM employee
LEFT JOIN time_entry
ON employee.id = time_entry.student_id
WHERE time_entry.date < 1367798400
GROUP BY employee.id;
I obtain an empty set. How can I use the WHERE
statement and still obtain 0 per employee when no entries are found in the database?