I want to select a set of employees from an attendance log between two dates who by that time have come to work for more than 10 days. So that the employer may have already exceeded 10 days or may exceed 10 days during the date range that is checked. The log table (see below) contains one record for each employer for everyday.
emp_id | date
1 | 2019-01-25
1 | 2019-01-26
1 | 2019-01-27
1 | 2019-01-28
1 | 2019-01-29
1 | 2019-01-30
1 | 2019-01-31
1 | 2019-02-01
1 | 2019-02-02
1 | 2019-02-03
1 | 2019-02-04
2 | 2019-01-29
2 | 2019-01-30
2 | 2019-01-31
2 | 2019-02-01
2 | 2019-02-02
2 | 2019-02-03
2 | 2019-02-04
2 | 2019-02-05
3 | 2019-01-23
3 | 2019-01-24
3 | 2019-01-25
3 | 2019-01-26
3 | 2019-01-27
3 | 2019-01-28
3 | 2019-01-29
3 | 2019-01-30
3 | 2019-01-31
3 | 2019-02-01
3 | 2019-02-02
Selecting date range 2019-02-01 to 2019-02-05.
- Emp 1 - By 2019-02-01 he has worked for 8 days.
- Emp 1 - By 2019-02-04 he has worked for 11 days.
- Emp 2 - By 2019-02-01 he has worked for 4 days.
- Emp 2 - By 2019-02-05 he has worked for 8 days.
- Emp 3 - By 2019-02-01 he has worked for 10 days.
- Emp 3 - By 2019-02-05 he has worked for 14 days.
So from the above data set only Emp 1 and Emp 3 should be selected. The employer has to exceed 10 days within the date range.
I have written the following query in MySQL but it does not do the work. How should it be written?
SELECT emp_id FROM log
WHERE date BETWEEn '2019-02-01' AND '2019-02-05'
GROUP BY emp_id
HAVING COUNT(emp_id) > 14;