Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

How do i group by department and count the number of days present monthly?

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?

0 likes
26 replies
ntraykov's avatar

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!

1 like
tomasosho's avatar

Please can you help format this better. Thanks

ntraykov's avatar

You want to format better the output or the query? If you want the output, tell me exactly what you need.

ntraykov's avatar
ntraykov
Best Answer
Level 11

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();
1 like
tomasosho's avatar

I am getting this

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1 no such function: DATE_FORMAT (SQL: select "department", DATE_FORMAT(date, "%Y-%b") as `month`, "name", SUM(present) AS days_present from "attendance" group by "department", MONTH(date))
tomasosho's avatar

done, do i need to switch to mysql?

I have this

$attendance = 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();
jlrdw's avatar

Does date hold an actual date, like 2020-03-07.

1 like
tomasosho's avatar

I switched to Mysql I'm getting this

Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1055 'sevico.attendance.date' isn't in GROUP BY (SQL: select `department`, DATE_FORMAT(date, "%Y-%b") as `month`, `name`, SUM(present) AS days_present from `attendance` group by `department`, MONTH(date))

My Controller

$date = DB::table('attendance')->select('date')->get();
        //dd($date);
        $attendance = 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();
ntraykov's avatar

Like @jlrdw said you must remove $date and replace it with date. You've changed the original query that I've sent.

tomasosho's avatar

Please check out my updated reply.

I switched to Mysql I'm getting this

Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1055 'sevico.attendance.date' isn't in GROUP BY (SQL: select `department`, DATE_FORMAT(date, "%Y-%b") as `month`, `name`, SUM(present) AS days_present from `attendance` group by `department`, MONTH(date))

My Controller

$date = DB::table('attendance')->select('date')->get();
        //dd($date);
        $attendance = 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();
tomasosho's avatar

config()->set('database.connections.your_connection.strict', false);

'your_connection' what does it mean?

jlrdw's avatar

In your case it would be, mysql I have different connections with different names.

You can see the documentation that talks about database connections.

Updated not default see top sentence. Sorry I am on mobile here not my desktop.

https://github.com/laravel/laravel/blob/master/config/database.php

Above edited also I'm just wondering if using the keyword date is causing a problem.

Try using ticks around date,

`date`
tomasosho's avatar

config()->set('database.connections.default.strict', false);

jlrdw's avatar

Try

config()->set('database.connections.default.strict', false);

and try

config()->set('database.connections.mysql.strict', false);

I've only done this with other named connections.

Edit: I tested with mysql, works:

config()->set('database.connections.mysql.strict', false);

That should be what the connection is named.

1 like
tomasosho's avatar

am i meant to use it in my controller?

config()->set('database.connections.mysql.strict', false);

        $attendance = 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();
jlrdw's avatar

Wherever your query is if it's in controller yes or if it's in model then yes for model.

tomasosho's avatar

Thanks, It's working... But i want a count for the days present not a sum

jlrdw's avatar

Change the aggreate to COUNT.

1 like
tomasosho's avatar
Illuminate\Support\Collection {#444 ▼
  #items: array:1 [▼
    0 => {#445 ▼
      +"department": "Melanine"
      +"month": null
      +"name": "Victor Okon"
      +"days_present": 1
    }
  ]
}
```
The month is null
tomasosho's avatar

My Month is null, it's not picking it from my date.

        $date = DB::table('attendance')->select('date')->get();
        $attendance = DB::table('attendance')
        ->select('department', DB::raw('DATE_FORMAT(`date`, "%Y-%b") as `month`'), 'name', DB::raw('count(present) AS days_present'))
        ->groupBy('department', DB::raw('MONTH(`date`)'))
        ->get();

Please or to participate in this conversation.