Good day. I make a request in Doctrine.
... ->addSelect('(CASE WHEN c.type = 1 THEN ( SUM(c.amountMain) AND COUNT(c.id) ) ELSE 0 END)') I want to return SUM (c.amountMain) and COUNT (c.id) when fulfilling the condition
Help please issue a request.
In general, actions need to be carried out separately. And you do not need to use COUNT () in this case, since all the results will be counted. SUM () should be used instead.
->addSelect('SUM((CASE WHEN c.type = 1 THEN c.amountMain ELSE 0 END) as sum') ->addSelect('SUM((CASE WHEN c.type = 1 THEN 1 ELSE 0 END) as count') Source: https://ru.stackoverflow.com/questions/622137/
All Articles
(case ... sum()), (case ... count()). And in general a little strange condition. Do you have a group by request in the c.type column? Just because you have written c.type in case should be checked for each line, i.e. before grouping, and sum () and count () will take many lines into account, i.e. After grouping - Mikesum(case ... then amountMain else 0 end)functionsum(case ... then amountMain else 0 end)- Mike