在SQL查询中,如何获得另一个SUM的SUM?

时间:2015-09-16 10:21:55

标签: sql function sum aggregate

SELECT b.BranchName, SUM(SUM(ob.FirstQty + ob.SecondQty))* ps.Price TotalCost
FROM Branch b, Product_Supplier ps, Product p, Supplier s, Order o, Order_Details od
WHERE o.BranchID = b.BranchID
AND o.OrderID = ob.OrderID
AND ob.ProductID = p.ProductID
AND p.FirstSupplierID = s.SupplierID
AND ps.SupplierID = s.SupplierID
AND ps.ProductID = ob.ProductID
AND b.BranchID = 1
AND o.OrderDate >= '2015-01-09'
AND o.OrderDate <= '2015-09-09'
Group By b.BranchName, ps.Price

我不知道如何获得另一笔款项的总和。所有的钥匙都是正确的。如果我删除了这样的第一笔金额,     SELECT b.BranchName,SUM(ob.FirstQty + ob.SecondQty)* ps.Price TotalCost 它有效,但它显示多种成本,如100,200,300等。我想要的是这些多种成本的总和(总成本)。

1 个答案:

答案 0 :(得分:0)

ps.Price子句中删除GROUP BY,并使用SUM函数内的价格进行乘法(使用一对额外的括号)。

查询变为:

SELECT b.BranchName, SUM((ob.FirstQty + ob.SecondQty) * ps.Price) TotalCost
FROM Branch b, Product_Supplier ps, Product p, Supplier s, Order o, Order_Details od
WHERE o.BranchID = b.BranchID
AND o.OrderID = ob.OrderID
AND ob.ProductID = p.ProductID
AND p.FirstSupplierID = s.SupplierID
AND ps.SupplierID = s.SupplierID
AND ps.ProductID = ob.ProductID
AND b.BranchID = 1
AND o.OrderDate >= '2015-01-09'
AND o.OrderDate <= '2015-09-09'
Group By b.BranchName