SUM的GROUP_CONCAT

时间:2015-03-23 09:44:56

标签: mysql group-by sum group-concat

我有两个select-queries,它们都能给出正确的结果:

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

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

sum1是所有sum2项的总和,我想要一个查询,它会给我sum1的结果和所有sum2项的GROUP_CONCAT项。但是如何在GROUP_CONCAT中为sum2 - items元素定义GROUP BY?

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

1 个答案:

答案 0 :(得分:0)

您应该使用子查询解决它,使用以下查询,我相信它将解决您的问题

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