【发布时间】:2015-05-08 07:52:38
【问题描述】:
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;
我想要各列最后一行中TotalItemsAmount、TotalTax、TotalCharges 和NetAmount 的总和。
在结果集中,我得到了每一行的重复,任何人都可以纠正我的代码中的错误。
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
【问题讨论】:
-
你能分享一些数据和你想要得到的结果吗?
-
没有适当的相同数据和预期输出,很难回答。您是否正在寻找类似
WITH ROLLUP的内容 -
为了获得最后一行中列的总和,我使用了 GROUP BY 和 ROLL UP 子句(如代码所示),但在执行时使用 NULL 生成每行的重复。我想避免重复。
标签: sql sql-server sql-server-2008 database-management