2013-06-27 78 views
2

以下查詢當前顯示的是總項目,但是我想要的是計算採購訂單中所有項目的小計。查詢中所有項目的小計

感謝

輸出應該是:

POID Item ItemQTY ItemPrice ItemTotal SubTotal 
1  A  1  15.00  15.00  80.50 
1  B  1  25.50  25.50  80.50 
1  C  2  20.00  40.00  80.50 
2  X  6  5.00  30.00  50.00 
2  Y  2  10.00  20.00  50.00 

關係purchase_order 1 - N purchase_order_items

SELECT 
purchase_order.id AS POID, 

purchase_order_items.description AS Item, 
purchase_order_items.quantity AS ItemQTY, 
purchase_order_items.price AS ItemPrice, 
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal 

/* Here, Subtotal should be calculated and displayed */ 

FROM purchase_order 
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 

我看着MySQL finding subtotalsSubtotals and SQL,但不能適用於我的查詢。

+0

你可以在你的服務器端腳本中用循環來計算這個值。或者,你可以在你的上面的查詢中做一個內部的'SELECT'。 –

+0

作爲一個單獨的值,它會更好,因爲小計在邏輯上對每行數據都沒有意義。您應該調用一個單獨的方法/ sql proc來獲取總數,或者通過代碼/其他方法計算返回的數據。 – Tanner

+0

@坦納 - 這是可能的。請參閱下面的接受答案。 – BentCoder

回答

1

嘗試

SELECT i.fk_purchase_order POID, 
     description   Item, 
     quantity   ItemQTY, 
     price    ItemPrice, 
     quantity * price ItemTotal, 
     s.subtotal   SubTotal 
    FROM purchase_order_items i JOIN 
(
    SELECT fk_purchase_order, SUM(quantity * price) subtotal 
    FROM purchase_order_items 
    GROUP BY fk_purchase_order 
) s ON i.fk_purchase_order = s.fk_purchase_order 

輸出:

 
| POID | ITEM | ITEMQTY | ITEMPRICE | ITEMTOTAL | SUBTOTAL | 
------------------------------------------------------------ 
| 1 | A |  1 |  15 |  15 |  80.5 | 
| 1 | B |  1 |  25.5 |  25.5 |  80.5 | 
| 1 | C |  2 |  20 |  40 |  80.5 | 
| 2 | X |  6 |   5 |  30 |  50 | 
| 2 | Y |  2 |  10 |  20 |  50 | 

這裏是SQLFiddle演示

+0

謝謝。有用。 – BentCoder

+0

不客氣。我很高興我可以幫助:) – peterm

0

怕我認爲最好的辦法是交叉連接對一個子查詢: -

SELECT 
purchase_order.id AS POID, 
purchase_order_items.description AS Item, 
purchase_order_items.quantity AS ItemQTY, 
purchase_order_items.price AS ItemPrice, 
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal, 
Sub1.FullTotal 
FROM purchase_order 
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 
CROSS JOIN 
(
    SELECT SUM(purchase_order_items.quantity*purchase_order_items.price) AS FullTotal 
    FROM purchase_order 
    INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 
) Sub1 

要拿回來分組採購訂單ID

SELECT 
purchase_order.id AS POID, 
purchase_order_items.description AS Item, 
purchase_order_items.quantity AS ItemQTY, 
purchase_order_items.price AS ItemPrice, 
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal, 
Sub1.FullTotal 
FROM purchase_order 
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 
INNER JOIN 
(
    SELECT purchase_order.id, SUM(purchase_order_items.quantity*purchase_order_items.price GROUP BY purchase_order.id) AS FullTotal 
    FROM purchase_order 
    INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 
) Sub1 
ON purchase_order.id = Sub1.id 
+0

由於我的錯誤,您的查詢返回所有采購訂單的小計。我的意思是,所有采購訂單都是單獨的。我更新了這個問題。 – BentCoder

+0

修改後的響應以匹配更新的要求。 – Kickstart

2

可以使用WITH ROLLUP功能來獲取小計:

SELECT 
purchase_order.id AS POID, 

purchase_order_items.description AS Item, 
purchase_order_items.quantity AS ItemQTY, 
purchase_order_items.price AS ItemPrice, 
SUM(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal 
FROM purchase_order 
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order 
GROUP BY POID, Item WITH ROLLUP 

這將創建一個具有Item = NULL爲寶分類彙總的結果集,並POID = NULL爲盛大總。這些小計和總計位於這些行的ItemTotal列中。

+0

非常有趣。我會記住的。 +1 – BentCoder

+0

有趣。 +1 – Kickstart

相關問題