Issue with translating SQL query into Database API (count(*))
As @Clive said, just add an expression to your first query. countQuery() is only about getting a single count for a query and in many cases it is very inefficient.

$con = \Database::getConnection('old_db');
$query = $con-&gt;select('PRESTATIONS', 'prestations');
$query-&gt;fields('prestations', ['LIBELLE']);
$query-&gt;addExpression('COUNT(*)', 'count');
$query-&gt;condition($group);
$query-&gt;condition('PRESTATION', 'AIDES', '=');
$query-&gt;groupBy('LIBELLE');

foreach ($query-&gt;execute() as $row) {
}