This is my table:
Supposing I am passing $cameras containing cameras ids 30 and 31 my result should be 70, it is sum of 30 (for id 43, it is max Id for camera_id 31) and 40 (id 45, it is max id for camera_id 32)
I can achieve this result with this SQL statement:
SELECT SUM(t.number)
FROM `count` t
INNER JOIN
(SELECT MAX(id) id, camera_id
FROM`count`
WHERE camera_id
IN $cameras
GROUP BY camera_id)t1
ON t1.id = t.id
But I would like to convert this SQL statement to Doctrine's query. This is what I try:
$this->createQueryBuilder("c")
->select('SUM(c.number)')
->innerJoin(Count::class, 'c2', 'WITH' , 'c.id = c2.id')
->select('MAX(c2.id)')
->where('c2.camera IN (:value)')
->setParameter('value', $cameras)
->groupBy('c2.camera')
->getQuery()
But this query returns to me Max ids and I want to these Max ids were returned to the
->andWhere('SUM(c.number)')
line where they should be summed.
How can I adjust my query code?
Thank you very much.