Sql select语句中的计算

Sql select语句中的计算,sql,Sql,通过下表的详细信息,我想列出订单ID和每个订单的总成本 Product (ProductID,ProductDescription, QtyInStock, ReOrderLevel, CostPrice, SellPrice) Order(OrderNum, OrderDate, DeliveryDate) OrderDetails (OrderDetailID, OrderNum, ProductID, Quantity) 我的尝试,我有 SELECT OrderNumber, s

通过下表的详细信息,我想列出订单ID和每个订单的总成本

Product (ProductID,ProductDescription, QtyInStock, ReOrderLevel, CostPrice, SellPrice)
Order(OrderNum, OrderDate, DeliveryDate)    
OrderDetails (OrderDetailID, OrderNum, ProductID, Quantity)
我的尝试,我有

SELECT OrderNumber, sum(SellPrice * Quantity) AS TotalCost
FROM OrderDetail
INNER JOIN Product
ON Product.ProductID = OrderDetail.ProductID

如何显示每个订单号的总和的结果?

您应该使用
分组方式对结果进行分组。现在你得到了所有的总数,并在每一行上显示出来

此代码将给出您期望的结果:

SELECT OrderNumber, sum(SellPrice * Quantity) AS TotalCost
FROM OrderDetail
INNER JOIN Product
ON Product.ProductID = OrderDetail.ProductID
group by OrderNumber

您缺少
groupby
OrderNumber`。