我有一张发票的表格:
invoice_num, customer_ID, usd
1 A 15.2
2 B 3.6
3 A 105.1
4 C 6.0
我需要一份报告,显示所有记录(发票)并为每位客户添加小计。我知道如果我只显示每个客户的总数(使用GROUP BY customer_ID和WITH ROLLUP),但是我需要保留详细信息,所以我无法对这些行进行分组。 所需的输出是:
invoice_num customer_ID usd
1 A 15.2
3 A 105.1
Total customer A 120.3
2 B 3.6
Total customer B 3.6
4 C 6.0
Total customer C 6.0
Total customers 129.9
谢谢,
答案 0 :(得分:15)
同时在invoice_num
上分组:
SELECT invoice_num, customer_ID, SUM(usd)
FROM my_table
GROUP BY customer_ID, invoice_num WITH ROLLUP
在sqlfiddle上查看。