MySQL - 转换简单表

时间:2014-07-25 00:46:48

标签: mysql transform

这看起来很简单但我觉得可能要实现很多步骤。有没有简单的方法呢?

enter image description here

1 个答案:

答案 0 :(得分:2)

您正在寻找聚合函数,尤其是group_concat()。关键是你要做两个聚合:

select subgroupcombo, count(*) as NumCustomers, sum(spend) as TotalSpend,
       sum(Transactions) as TotalTransactions
from (select customer, group_concat(subgroup order by subgroup) as subgroupcombo,
             sum(spend) as spend, sum(transactions) as transactions
      from input i
      group by customer
     ) c
group by subgroupcombo;