我将几乎使用下表和数据,并添加净值,商品及服务税和总值列:
CustomerPO| Net | GST | Gross
DS4284735 100 50 150
DS4284735 70 30 100
我的预期结果将是:
CustomerPO| Net | GST | Gross
DS4284735 170 80 250
我该如何实现?
我的选择代码是:
SELECT dbo.Invoice.CustomerPO AS Reference, dbo.InvoiceDetails.ExtendedPrice AS Net, dbo.InvoiceDetails.TotalTaxes AS GST,
dbo.InvoiceDetails.ExtendedPrice + dbo.InvoiceDetails.TotalTaxes AS Gross
我是否必须对所有内容进行分组?请注意,这两个数据库表中的InvoiceID有所不同
答案 0 :(得分:0)
SELECT ih.CustomerPO AS Reference,
sum(id.ExtendedPrice) AS Net,
sum(id.TotalTaxes) AS GST,
sum(id.ExtendedPrice + id.TotalTaxes) AS Gross
from dbo.Invoice ih
inner join dbo.InvoiceDetails id on ih.InvoiceId = id.InvoiceId
group by ih.CustomerPO;
您只能按非汇总字段CustomerPO分组。
答案 1 :(得分:0)
SELECT dbo.Invoice.CustomerPO AS Reference
, ISNULL(dbo.Invoice.CredInvoiceNo, dbo.Invoice.InvoiceNo)
AS [Invoice Number]
, AVG(dbo.InvoiceDetails.ExtendedPrice) AS Net
, AVG(dbo.InvoiceDetails.TotalTaxes) AS GST
, CONVERT(int, ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice + dbo.InvoiceDetails.TotalTaxes), 0)) AS Gross
GROUP BY dbo.Invoice.InvoiceNo, dbo.Invoice.CustomerPO, ISNULL(dbo.Invoice.CredInvoiceNo, dbo.Invoice.InvoiceNo), dbo.Invoice.EntryDate