$queryBuilder = $this->createQueryBuilder('account_event'); $queryBuilder ->select('account_event.id AS AE_id', 'IDENTITY(account_event.event) AS event_id', 'account_event.dueDate as due_date', 'IDENTITY(account_event.account) AS account_id') ->where( $queryBuilder->expr()->in( 'account_event.id', $this->createQueryBuilder('subquery_repository') ->select('MAX(ae2.id)') ->from(AccountEvent::class,'ae2') ->andWhere('ae2.event IN (:events)') ->andWhere('ae2.account IN (:accounts)') ->andWhere('ae2.dueDate > DATE(:date)') ->groupBy('ae2.account') ->getDQL() ) ) ->setParameter('events', $events) ->setParameter('accounts', $accounts) ->setParameter('date', $date->format('Y-m-d')) ;