0

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;
3
  • can you post your table structure and few rows of data? Commented Feb 7, 2019 at 8:07
  • 1
    update your question and add the tables schema a proper data sample and the expected result .. (your actual data sample is not useful) Commented Feb 7, 2019 at 8:08
  • Added table data Commented Feb 7, 2019 at 8:40

2 Answers 2

1

You can split the date condition between WHERE and HAVING:

SELECT emp_id
FROM log
WHERE date <= '2019-02-05' -- select all entries until this date
GROUP BY emp_id
HAVING MAX(date) >= '2019-02-01' -- employee has at least one log entry on / after this date
AND COUNT(emp_id) > 10 -- more than 10 entries between the two dates
Sign up to request clarification or add additional context in comments.

Comments

0

I think you want records of employees whom working days are greater and equal to 10. So you can apply sub query which can formulate count of those employees and return result back to main query.

SELECT l1.emp_id FROM log l1
WHERE (select Count(*) from log l2 
         where l1.emp_id=l2.emp_id 
          && l2.date BETWEEN '2019-02-01' AND '2019-02-05') >=10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.