将行插入查询结果(总和)

时间:2011-04-19 16:12:07

标签: sql

  

可能重复:
  Insert line into a query result (sum)

我的报告显示了客户订购的产品及其价格:

CompanyA    Product 7    14.99  
CompanyA    Product 3    45.95
CompanyA    Product 4    12.00
CompanyB    Product 3    45.95
CompanyC    Product 7    14.99
CompanyC    Product 3    45.95

我想插入一行汇总每个公司的订单,如下所示:

CompanyA    Product 7    14.99  
CompanyA    Product 3    45.95
CompanyA    Product 4    12.00
               Total:    72.94
CompanyB    Product 3    45.95
               Total:    45.95
CompanyC    Product 7    14.99
CompanyC    Product 3    45.95
               Total:    60.94

这里有一些代码显示了我的查询的基本结构:

SELECT company
   , product
   , price
FROM companyMaster
ORDER BY company,
   , product,
   , price;

有谁知道怎么做?我在Transact-SQL(Microsoft SQL Server)中写这个。

1 个答案:

答案 0 :(得分:1)

您需要两个不同的“分组”查询,一个是客户,产品,另一个是客户。