Нужно выполнить сложный запрос в Doctrine, но не получается по тому как выходит вот такая ошибка:
[Semantical Error] line 0, col 549 near 'FROM (': Error: Subquery is not supported here
Вот DQL запрос:
SELECT SUM(t.absentDay) AS absentDayAll
FROM (
SELECT COUNT(DISTINCT DATE_FORMAT(voting.dateAt, '%Y-%m-%d')) AS absentDayHuman
FROM \App\Entity\Vote vote
JOIN vote.human human
JOIN vote.voting voting
JOIN human.party party
WHERE vote.type = 0
GROUP BY human.id
) AS t
Вот аналогичный запрос на SQL:
SELECT SUM(t.absentDay) as absentDayAll
FROM (
SELECT COUNT(DISTINCT DATE_FORMAT(voting.date_at, '%Y-%m-%d')) AS absentDayHuman
FROM vote
JOIN human ON human.id = vote.human_id
JOIN voting ON voting.id = vote.voting_id
JOIN party ON party.id = human.party_id
WHERE vote.type = 0
GROUP BY human.id
) AS t
И Docrtine Native Query не подойдёт т.к. этот запрос(и подобные ему) предполагается добавлять как одно из полей к более сложным запросам:
$parties = $partyRepository->createQueryBuilder('party')
->addSelect("(
SELECT SUM(q3_absent.absentDay)
FROM (
SELECT COUNT(DISTINCT DATE_FORMAT(q3_voting.dateAt, '%Y-%m-%d')) AS absentDay
FROM " . Vote::class . " q3_vote
JOIN q3_vote.human q3_human
JOIN q3_vote.voting q3_voting
JOIN q3_human.party q3_party
WHERE vote.type = :typeAbsent AND q3_party.id = party.id
GROUP BY human.id
) AS q3_absent
) AS countAbsentDay")
->setParameter('typeAbsent', Vote::TYPES_CODE[Vote::TYPE_ABSENT])
->getQuery()
->getResult()
;