@ligonsker You can try this:
DB::table('table')
->select('group', 'unit', 'department', DB::raw('NULL as team'), DB::raw('COUNT(*) as rows'))
->where([
['group', '=', 'g1'],
['unit', '=', 'u1'],
['department', '=', 'd1'],
])
->groupBy('group', 'unit', 'department')
->union(DB::table('table')
->select('group', 'unit', 'department', 'team', DB::raw('COUNT(*) as rows'))
->where([
['group', '=', 'g25'],
['unit', '=', 'u54'],
['department', '=', 'd70'],
['team', '=', 't88'],
])
->groupBy('group', 'unit', 'department', 'team')
)
->get();
Or you can use this also:
$firstQuery = DB::table('table')
->select('group', 'unit', 'department', DB::raw('NULL as team'), DB::raw('COUNT(*) AS rows'))
->where('group', 'g1')
->where('unit', 'u1')
->where('department', 'd1')
->groupBy('group', 'unit', 'department');
$secondQuery = DB::table('table')
->select('group', 'unit', 'department', 'team', DB::raw('COUNT(*) AS rows'))
->where('group', 'g25')
->where('unit', 'u54')
->where('department', 'd70')
->where('team', 't88')
->groupBy('group', 'unit', 'department', 'team');
$result = $firstQuery->unionAll($secondQuery)->get();
ℹ️ Don't forget to eager loading (where necessary) in order to reduce the number of queries to improve the performance.