如何获得最后一行列的总和

how to get the summation of column in the last row

select csm.csmCustomerName, cur.curNameOfCurrency, 
sum(sot.sotItemTotalAmount)as 'TotalItemsAmount',
SUM(sorTotalTaxAmountValue) as 'TotalTax',
SUM(sorTotalChargeDetailsAmountValue) as 'TotalCharges',
(sum(sorTotalTaxAmountValue)+sum(sorTotalChargeDetailsAmountValue)+sum(sot.sotItemTotalAmount)) as 'NetAmount'
from dbo.SalesOrder sor join dbo.Currency cur
on sor.sorCurrencyId=cur.curId
join dbo.CustomerMaster csm
on sor.sorCustomerMasterId=csm.csmId
join SalesOrderItemDetails sot
on sot.sotSalesOrderId=sor.sorId
Group by csmCustomerName, curNameOfCurrency with rollup;

我想要各列最后一行中的 TotalItemsAmountTotalTaxTotalChargesNetAmount 的总和。 在结果集中,我得到了每一行的重复,谁能纠正我代码中的错误。


C1 C2 C3 C4 C5 C6

1 美元 7 2 10

B 美元 3 6 3 12

C 美元 5 3 0 8

D 美元 4 2 1 7

   13  18   6  37

它是通过像这样的分组集完成的:

DECLARE @t TABLE ( code CHAR(3), a INT, b INT )

INSERT  INTO @t
VALUES  ( 'USD', 1, 2 ),
        ( 'USD', 5, 1 ),
        ( 'USD', 10, 7 ),
        ( 'EUR', 15, 13 )


SELECT  code ,
        SUM(a) AS a ,
        SUM(b) AS b
FROM    @t
GROUP BY GROUPING SETS(( code ), ( ))

输出:

code    a   b
EUR     15  13
USD     16  10
NULL    31  23