9

我有一张发票表:

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

谢谢,

4

1 回答 1

16

还分组invoice_num

SELECT   invoice_num, customer_ID, SUM(usd)
FROM     my_table
GROUP BY customer_ID, invoice_num WITH ROLLUP

sqlfiddle上查看。

于 2013-02-09T10:12:31.403 回答