2010-08-17 58 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