Search code examples
mysqlselectcountdistinct

count distintc date and employee_id


I have table like this:

date_entry emp_id
2024-02-01 emp-001
2024-02-01 emp-001
2024-02-01 emp-002
2024-02-01 emp-003
2024-02-02 emp-001
2024-02-02 emp-002
2024-02-02 emp-003
2024-02-03 emp-001
2024-02-03 emp-002
2024-02-03 emp-002
2024-02-03 emp-003
2024-02-04 emp-002
2024-02-04 emp-003

I need to count the total of the day employee attend and remove the duplicate entry from date range like this:

date range 2024-02-01 until 2024-02-04:

emp_id day_attend
emp-001 3
emp-002 4
emp-003 4

how to query in mysql? please help me, thank you.


Solution

  • Simple and straightforward.

    SELECT 
        emp_id, 
        COUNT(DISTINCT date_entry) AS day_attend
    FROM 
        YourTableName
    WHERE 
        date_entry BETWEEN '2024-02-01' AND '2024-02-04'
    GROUP BY 
        emp_id
    ORDER BY 
        emp_id;