Hello!
Could you please try with that:
SELECT department, DATE_FORMAT(date, "%Y-%b") as `month`, name, SUM(present) AS days_present
FROM attendance
GROUP BY department, MONTH(date);
Greetings!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
My Table
CREATE TABLE attendance (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
date DATE,
name VARCHAR,
department VARCHAR,
present VARCHAR,
absent VARCHAR,
created_at DATETIME,
updated_at DATETIME,
staff_id VARCHAR
);
How do i group it by department and count the number of days present monthly?
Just include the columns you are interested in.
DB::table('attendance')
->select('department', DB::raw('DATE_FORMAT(date, "%Y-%b") as `month`'), 'name', DB::raw('SUM(present) AS days_present'))
->groupBy('department', DB::raw('MONTH(date)'))
->get();
Please or to participate in this conversation.