2013-02-22 39 views
0

我有一個MySQL數據庫三個相關表:SUM()的多個值相關表

  • inventory_items定義倉庫物品的種類
  • inventory_transactions定義廣告的添加或刪除
  • 數量
  • 的位置上確定的位置說庫存

Inventory_transactions有許多存儲在給定inventory_item如何添加或刪除一個quantity_offset從給定的位置。

我有以下查詢,它將獲得global_quantity(給定inventory_item_id的所有quantity_offsets的總和)以及location_quantity,只要知道位置ID,它就可以爲特定位置執行相同的操作。

SELECT sku, name, SUM(quantity_offset) AS global_quantity, 
(
SELECT SUM(quantity_offset) 
    FROM inventory_transactions 
    WHERE inventory_items.inventory_item_id = inventory_transactions.inventory_item_id AND inventory_transactions.location_id = 1 
) AS location_quantity 
FROM inventory_items 
JOIN inventory_transactions ON inventory_items.inventory_item_id = inventory_transactions.inventory_item_id 
GROUP BY sku 

我想用被添加到每個位置的列結束了,所以結果可能有值,例如:

sku, name, global_quantity, denver_quantity, dallas_quantity, ft_wayne_quantity 

我的回退是要做到這一點對個人inventory_item頁面(當我就知道庫存物品,可以通過定位組),但能做到在一個單一的查詢,並把它全部納入一個表將是非常巨大的。

起初,我以爲我可以通過迭代子查詢,但無法設置列名(AS)編程。

非常感謝您提供任何幫助。

回答

1

順便說一句,而不是使用子查詢SELECT列表中,你可以得到與上如果行是從給定的位置,有條件地返回quantity_offset的計算式的SUM聚合相同的結果。我也將使用左外連接到inventory_transactions表,這樣我就一定要得到一個回行的每一行中inventory_items:

SELECT i.sku 
    , i.name 
    , SUM(t.quantity_offset) AS global_quantity 
    , SUM(IF(t.location_id = 1,t.quantity_offset,NULL)) AS location_1_quantity 
    FROM inventory_items i 
    LEFT 
    JOIN inventory_transactions t ON i.inventory_item_id = t.inventory_item_id 
    GROUP BY i.sku 

(這不回答你的問題,但我想讓你意識到這一點)。

要回答你的問題,首先,SELECT語句無法動態地改變它在結果集中返回的列的數量或類型。你需要定義你想返回的每一列的表達式。 (在SQL SELECT statment文字的產生可動態完成,產生特定的SELECT語句,而是由SELECT語句返回的列由SELECT語句固定。)

如果我有一個相對固定的LOCATION_ID價值觀,我需要的結果集指定,我會重複的條件表達式多次的總和,每進行一次位置或什麼的,我想位置設置:

SELECT i.sku 
    , i.name 
    , SUM(t.quantity_offset) AS global_quantity 
    , SUM(IF(t.location_id = 1,t.quantity_offset,NULL)) AS location_1_quantity 
    , SUM(IF(t.location_id = 2,t.quantity_offset,NULL)) AS location_2_quantity 
    , SUM(IF(t.location_id = 3,t.quantity_offset,NULL)) AS location_3_quantity 
    , SUM(IF(t.location_id IN (1,2,3),t.quantity_offset,NULL)) AS location_123_quantity 
    , SUM(IF(t.location_id = 4,t.quantity_offset,NULL)) AS location_4_quantity 
    FROM inventory_items i 
    LEFT 
    JOIN inventory_transactions t ON i.inventory_item_id = t.inventory_item_id 
    GROUP BY i.sku 

如果要返回的列的集合需要真正動態,那麼我不會將它們作爲列返回。相反,我會將每個位置的SUM(quantity_offset)作爲單獨的行返回,然後在客戶端處理轉換爲列式表示。

+0

謝謝!基於我知道查詢運行前的位置數(但不是在寫入時)的事實,我想我會嘗試在應用程序接觸數據庫之前生成SELECT。在select中添加可變數量的條件和應該很好。 – 2013-02-25 17:36:19

0

試試這個

SELECT sku, name, SUM(quantity_offset) AS global_quantity, 
     (
     SELECT SUM(quantity_offset) 
     FROM inventory_transactions 
      WHERE inventory_items.inventory_item_id = inventory_transactions.inventory_item_id AND inventory_transactions.location_id = 1 
      AND location = denver) AS denver_quantity , 
     (
     SELECT SUM(quantity_offset) 
     FROM inventory_transactions 
      WHERE inventory_items.inventory_item_id = inventory_transactions.inventory_item_id AND inventory_transactions.location_id = 1 
      AND location = dallas) AS dallas_quantity, 
     (
     SELECT SUM(quantity_offset) 
     FROM inventory_transactions 
      WHERE inventory_items.inventory_item_id = inventory_transactions.inventory_item_id AND inventory_transactions.location_id = 1 
      AND location = ft_wayne) AS ft_wayne_quantity 
    FROM inventory_items 
    JOIN inventory_transactions ON inventory_items.inventory_item_id = inventory_transactions.inventory_item_id 
    GROUP BY sku 

注意我用了location作爲列名我不知道你有什麼位置的列名,只是更換

1

您應該使用有條件的資金 - 這是最有效的方式(最快的查詢)我知道的:

SELECT sku, name, SUM(quantity_offset) AS global_quantity, 
    sum(case when inventory_transactions.location_id = 1 then quantity_offset else 0 end) as loc1_quantitity, 
    sum(case when inventory_transactions.location_id = 2 then quantity_offset else 0 end) as loc2_quantitity, 
    sum(case when inventory_transactions.location_id = 3 then quantity_offset else 0 end) as loc3_quantitity 
FROM inventory_items JOIN inventory_transactions ON inventory_items.inventory_item_id = inventory_transactions.inventory_item_id 
GROUP BY sku 
+0

我不知道有條件的總和。直到現在,我已經選擇了數據,並以相對的方式顯示它(存儲的方式相同)。感謝你的分享! – 2013-02-25 17:36:58