Search code examples
mysqlgroup-bysumgroup-concat

GROUP_CONCAT of SUMs


I have two select-queries which both give correct results:

SELECT SUM(value) AS "sum1" FROM table GROUP BY id1

and

SELECT SUM(value) AS "sum2" FROM table GROUP BY id1, id2

sum1 is the sum of all sum2-items and I want a query that gives me a result of sum1 and a GROUP_CONCAT of all sum2-items. But how can I define the GROUP BY inside the GROUP_CONCAT for the sum2-items element?

SELECT SUM(value) AS "sum1", GROUP_CONACAT(SUM(value) AS "sum2" ... (?))
FROM table GROUP BY id1

Solution

  • You should solve it using sub query, use the following query, i believe it will solve your problem

    select sum(t.part_sum) as sum1, group_concat(t.part_sum) as sum2 from (select sum(value) as part_sum from table group by id1, id2) as t