简体   繁体   English

MySQL:当日期计数超过给定数量时,选择日期范围内的数据

[英]MySQL: Select data in a date range when date count exceeds a given amount

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. 我想从两个日期之间的出勤日志中选择一组员工,这些员工到那时已经工作了10天以上。 So that the employer may have already exceeded 10 days or may exceed 10 days during the date range that is checked. 这样,在检查的日期范围内,雇主可能已经超过10天,或者可能超过10天。 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. 选择日期范围2019-02-01至2019-02-05。

  • Emp 1 - By 2019-02-01 he has worked for 8 days. Emp 1-到2019-02-01他已经工作了8天。
  • Emp 1 - By 2019-02-04 he has worked for 11 days. Emp 1-到2019-02-04他已经工作了11天。
  • Emp 2 - By 2019-02-01 he has worked for 4 days. Emp 2-到2019-02-01他已经工作了4天。
  • Emp 2 - By 2019-02-05 he has worked for 8 days. Emp 2-到2019-02-05为止,他已经工作了8天。
  • Emp 3 - By 2019-02-01 he has worked for 10 days. Emp 3-到2019-02-01他已经工作了10天。
  • Emp 3 - By 2019-02-05 he has worked for 14 days. Emp 3-到2019-02-05他已经工作了14天。

So from the above data set only Emp 1 and Emp 3 should be selected. 因此,从以上数据集中,仅应选择Emp 1和Emp 3。 The employer has to exceed 10 days within the date range. 雇主必须在该日期范围内超过10天。

I have written the following query in MySQL but it does not do the work. 我已经在MySQL中编写了以下查询,但无法完成工作。 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;

You can split the date condition between WHERE and HAVING : 您可以在WHEREHAVING之间分割日期条件:

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

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. 我认为您需要工作日大于等于10的员工的记录,因此您可以应用子查询来制定这些员工的人数并将结果返回到主查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM