2010-08-17 54 views
6

我有两个表如下计算的(数量*价格)从2个不同的表

PRODUCT

Id | Name | Price 

之和的ORDERITEM

Id | OrderId | ProductId | Quantity 

我是什么试图做的是,计算每个产品的小计价格(数量*价格),然后计算整个订单的总价值。

我想喜欢的事,这

SELECT Id, SUM(Quantity * (select Price from Product where Id = Id)) as qty 
FROM OrderItem o 
WHERE OrderId = @OrderId 

当然,这并不工作:)

赞赏任何帮助的,但!

编辑:我只想显示整个订单的总计,所以基本上是OrderItem中每一行的Quantity * Price的总和。以下是一些示例数据。

示例数据

表产品

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 

中号

+0

你需要两列 - 每个产品的小计和每个订单的总数?您的查询只有一列... – 2010-08-17 19:54:59

+2

当询问有关SQL查询的建议时,最好包含少量样本数据(即使只有3或4行)以及查询的预期结果。 – 2010-08-17 20:29:12

+0

嗨汤姆 - 现在已添加示例数据。 – Marko 2010-08-17 21:07:42

回答

17

用途:

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.quantityp.price为空,SUM将返回NULL。

+0

+1以确保正确性 - 并剥去您的基本结构。 – Randy 2010-08-17 19:57:04

+1

+1但是我认为“如果oi.quantity'或'p.price'为空,那么SUM将返回NULL”的语句有点不清楚。如果组的_all_行的值为null,Sum将仅返回null。否则sum忽略null。因此,对于该组中的每一行,数量或价格必须为空,以使该组为空。 – 2010-08-17 20:57:22

+0

嗨@OMG Ponies,您的查询返回每个OrderItem行的小计,而不是所有行的总计。几乎像SUM被忽略:/ – Marko 2010-08-17 21:12:06

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 
+0

嗨@Beth - 我在关键词'GROUP'附近收到了一个错误的语法。“ – Marko 2010-08-17 21:18:47

+0

对不起,需要在之后添加一个别名) – Beth 2010-08-17 21:26:46

+0

这会返回OrderItem表中的每一行,并且order_total和小计是相同的,所以SUM再次不做它的工作:/ – Marko 2010-08-17 21:35:51

1

我认为这 - 包括空值= 0

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 
+1

'NVL'是Oracle特有的 - COALESCE将是更好的选择。 – 2010-08-17 19:59:53

+0

没有理由在每个输入上执行COALESCE(或NVL)。你可以:合并(sum(oi.quantity * p.price),0)作为total_qty'。 – 2010-08-17 20:58:44

+0

这不会返回总计,而是每个OrderItem行的小计,与上面的@OMG Ponies的回答相同。 – Marko 2010-08-17 21:15:47

1

我认为这是一起的,你在找什么线路。看起来您希望查看订单中每个商品的小计和订单的总金额。

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 
+0

嘿@mpminnich,我只需要所有OrderItems的总计,所以基本上SUM的Quantity *价钱 – Marko 2010-08-17 21:14:16

0

我有同样的问题,因为马尔科和遇到这样一个解决方案:

/*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存储过程来获取总计:)

EXEC GetGrandTotal