这看起来很简单但我觉得可能要实现很多步骤。有没有简单的方法呢?
答案 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;