通过总和查询混淆了我的小组

时间:2014-07-31 16:54:10

标签: tsql sql-server-2008-r2

这是我的第一个小组,我尝试执行以下操作:

  1. 按分支,帐户和参考编号分组
  2. 将分组的项目添加到一起,然后将组列TaxAmount和Amount汇总为总列。
  3. 这是我所拥有的,但它没有编译:

    SELECT        SUM(SUM(Amount) + SUM(TaxAmount)) AS Total
    FROM            Truck.PpsTransaction
    GROUP BY ReferenceNumber, Branch, AccountNumber
    

    我做错了什么?

2 个答案:

答案 0 :(得分:2)

SELECT ReferenceNumber, Branch, AccountNumber, SUM(Amount + TaxAmount) AS Total
  FROM Truck.PpsTransaction
 GROUP BY ReferenceNumber, Branch, AccountNumber

答案 1 :(得分:0)

在这种情况下,Rollup应该可以正常运行:

SELECT   ReferenceNumber, Branch, AccountNumberReferenceNumber,
         SUM(Amount) GroupAmount, Sum(TaxAmount), GroupTax,
         SUM(Amount + TaxAmount) AS GroupTotal
FROM     Truck.PpsTransaction
GROUP BY ReferenceNumber, Branch, AccountNumber WITH ROLLUP