Mysql嵌套查询和GROUP BY

时间:2014-03-28 14:07:12

标签: mysql sql nested-queries

我正在尝试对我的数据库执行以下查询: -

SELECT
source, Month as t1,
GROUP_CONCAT(SELECT SUM(amount) FROM `reports` GROUP BY Month) as amount
FROM `reports`
GROUP BY source

获取1个月内由不同来源获得的金额总和的sourcemonthconcatenated string。但是我收到了语法错误。

1 个答案:

答案 0 :(得分:1)

我不确定你需要什么,希望它是这两个中的一个:

SELECT source, Month, SUM(amount) as sum
FROM reports
GROUP BY source, Month

以上,但按来源分组,并在一个字段中列出了总和:

SELECT source, GROUP_CONCAT(sum) as sums
FROM (
    SELECT source, Month, SUM(amount) as sum
    FROM reports
    GROUP BY source, Month
) as t
GROUP BY source