Sql 从两个不同的表中计算(数量*价格)的总和

Sql 从两个不同的表中计算(数量*价格)的总和,sql,sum,Sql,Sum,我有两张桌子如下 产品表格 Id | Name | Price Id | OrderId | ProductId | Quantity 和一个ORDERITEM表 Id | Name | Price Id | OrderId | ProductId | Quantity 我想做的是,计算每个产品的小计价格(数量*价格),然后计算整个订单的总价值 我正在尝试这样的事情 SELECT Id, SUM(Quantity * (select Price from Product where Id

我有两张桌子如下

产品
表格

Id | Name | Price
Id | OrderId | ProductId | Quantity
和一个
ORDERITEM

Id | Name | Price
Id | OrderId | ProductId | Quantity
我想做的是,计算每个产品的小计价格(数量*价格),然后计算整个订单的总价值

我正在尝试这样的事情

SELECT Id, SUM(Quantity * (select Price from Product where Id = Id)) as qty
FROM OrderItem o
WHERE OrderId = @OrderId
但这当然行不通:)

感谢您的帮助

编辑:我只想显示整个订单的总计,因此基本上是OrderItem中每行的数量*价格之和。下面是一些示例数据

样本数据

表格产品

Id     Name            Price  
1      Tomatoes        20.09    
4      Cucumbers       27.72    
5      Oranges         21.13    
6      Lemons          20.05
7      Apples          12.05
表OrderItem

Id         OrderId        ProductId        Quantity
151        883            1                22
152        883            4                11
153        883            5                8
154        883            6                62
M

使用:

  SELECT oi.orderid,
         SUM(oi.quantity * p.price) AS grand_total,
    FROM ORDERITEM oi
    JOIN PRODUCT p ON p.id = oi.productid
   WHERE oi.orderid = @OrderId
GROUP BY oi.orderid

请注意,如果
oi.quantity
p.price
为空,则总和将返回空值。

我认为这一点-包括空值=0

select orderID, sum(subtotal) as order_total from
(
    select orderID, productID, price, qty, price * qty as subtotal
    from product p inner join orderitem o on p.id = o.productID
    where o.orderID = @orderID
) t
group by orderID
 SELECT oi.id, 
         SUM(nvl(oi.quantity,0) * nvl(p.price,0)) AS total_qty 
    FROM ORDERITEM oi 
    JOIN PRODUCT p ON p.id = oi.productid 
   WHERE oi.orderid = @OrderId 
GROUP BY oi.id 

我想这正是你想要的。您似乎希望查看订单ID、订单中每个项目的小计以及订单的总金额

select o1.orderID, o1.subtotal, sum(o2.UnitPrice * o2.Quantity) as order_total from
(
    select o.orderID, o.price * o.qty as subtotal
    from product p inner join orderitem o on p.ProductID= o.productID
    where o.orderID = @OrderId
)as o1
inner join orderitem o2 on o1.OrderID = o2.OrderID
group by o1.orderID, o1.subtotal

我遇到了与Marko相同的问题,并遇到了如下解决方案:

/*Create a Table*/
CREATE TABLE tableGrandTotal
(
columnGrandtotal int
)

/*Create a Stored Procedure*/
CREATE PROCEDURE GetGrandTotal
AS

/*Delete the 'tableGrandTotal' table for another usage of the stored procedure*/
DROP TABLE tableGrandTotal

/*Create a new Table which will include just one column*/
CREATE TABLE tableGrandTotal
(
columnGrandtotal int
)

/*Insert the query which returns subtotal for each orderitem row into tableGrandTotal*/
INSERT INTO tableGrandTotal
    SELECT oi.Quantity * p.Price AS columnGrandTotal
        FROM OrderItem oi
        JOIN Product p ON oi.Id = p.Id

/*And return the sum of columnGrandTotal from the newly created table*/    
SELECT SUM(columnGrandTotal) as [Grand Total]
    FROM tableGrandTotal
只需使用GetGrandTotal存储过程检索总计:)


是否需要两列-每个产品的小计和每个订单的总计?您的查询只有一列…在询问SQL查询的建议时,最好包含少量样本数据(即使只有3或4行)和查询的预期结果。嗨,Tom-现在已经添加了样本数据。示例数据为Thx,但输出的预期值是多少?订单的总计,所以我只想返回1个值——每个OrderItem的QuantityPrice之和,由OrderId过滤。简单地说,我想要求和(QuantityPrice),其中OrderId=883
NVL
是Oracle特有的-合并将是一个更好的选择。没有理由对每个输入进行合并(或NVL)。您可以:
合并(sum(oi.quantity*p.price),0)为总数量
。这不会返回总计,而是每个OrderItem行的小计,与上面@OMG Ponies的回答相同。+1但是我认为“如果
oi.quantity
p.price
为空,那么sum将返回空”的语句有点不清楚。仅当组中所有行的值为null时,Sum才会返回null。否则sum忽略null。因此,对于组中的每一行,数量或价格必须为null,才能使该组为null。嘿@OMG Ponies,您的查询将返回每个OrderItem行的小计,而不是所有行的总计。。几乎就像忽略了总和一样:/Hey@mpminnich,我只需要所有OrderItems的总计,因此基本上数量的总和*PriceHi@Beth-我得到了“关键字'GROUP'附近的语法不正确”。“对不起,需要在后面添加一个别名。)这将返回OrderItem表中的每一行,并且order_total和subtotal是相同的,因此,再次求和并不能完成它的工作:/抱歉,我不知道您只需要总计,我需要从内部查询中删除列,您不希望分解这些列。