Question

I currently have this query:

SELECT ((count(*) DIV 20) * 10) AS money_earned 
FROM ticket 
WHERE 
    closed_by = 269 AND 
    status = 1 AND
    closed_index >= TO_DAYS("2012/01/01") AND closed_index <= TO_DAYS("2013/01/01")
GROUP BY closed_index;

It yields this:

money_earned

60
50
30
20
20

Is there any way to either sum these rows, or concatenate the resulting rows into a string. I have tried to use GROUP_CONCAT, but get an "invalid use of Group function" error.

Instead, I would like to yield the following in a single query if possible:

money_earned
180

or

money_earned
60,50,30,20,20
Was it helpful?

Solution

You can use your query as a subquery:

select sum(money_earned), group_concat(money_earned)
from (SELECT ((count(*) DIV 20) * 10) AS money_earned 
      FROM ticket 
      WHERE closed_by = 269 AND 
            status = 1 AND
            closed_index >= TO_DAYS("2012/01/01") AND closed_index <= TO_DAYS("2013/01/01")
      GROUP BY closed_index
     ) tci;

Because of the rounding, I would be wary of doing this in a single query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top