2014-02-17 55 views
0

我需要幫助的東西似乎對我來說很複雜。多個SQL查詢或光標?

我做了一個查詢來創建一個tbl1,它是表Item和Warehouse的Cartesian乘積。它給的我帶回的所有項目中的所有倉庫:

SELECT i.ItemID, w.WarehouseID 
FROM Item i, Warehouse w 

我做了,我檢查了最後一個文檔以前或等於變量日期(@datevar),其數量規則之日起第二個查詢(TBL2) 1(PhysicalQtyRule = 1),這由項目和倉庫,從StockHistory表獲得現在

SELECT MAX(CreateDate) AS [DATE1], ItemID, Quantity, WarehouseID 
FROM StockHistory 
WHERE PhysicalQtyRule = 1 AND CreateDate <= @datevar 
GROUP BY ItemID, Quantity, WarehouseID 

,我需要更多的三個步驟:

  1. 生成每個項目和倉庫含有第三表數量總和,但數量規則爲2(PhysicalQtyRule = 2)和tbl2.date(如果存在)與變量@datevar之間的日期,從表StockHistory獲取。類似的東西:

    SELECT ItemID, WarehouseID, SUM(Quantity) 
    FROM StockHistory 
    WHERE PhysicalQtyRule = 2 AND CreateDate > tbl2.DATE1 --If exists 
    AND CreateDate <= @datevar 
    GROUP BY ItemID, WarehouseID 
    
  2. 生成每個項目和倉庫含有量的總和的第四表中,但數量的規則是3(PhysicalQtyRule = 3)和tbl2.date之間日期(如果有的話)和日期變量@datevar,從表StockHistory獲得。類似的東西:

    SELECT ItemID, WarehouseID, SUM(Quantity) 
    FROM StockHistory 
    WHERE PhysicalQtyRule = 3 AND CreateDate > tbl2.DATE1 --If exists 
    AND CreateDate <= @datevar 
    GROUP BY ItemID, WarehouseID 
    
  3. 創建基於第一個的決賽桌,與和數量列,這樣的事情:

    SELECT i.ItemID, w.WarehouseID, tbl2.Quantity + tbl3.Quantity – tbl4.Quantity AS [Qty] 
    FROM Item i, Warehouse w 
    

我不知道是否需要遊標(對我而言是新事物)或多個查詢,但重要的是性能最好,因爲我的StockHistory表有數百萬條記錄。

任何人都可以幫助我嗎?謝謝!

一些樣本數據中,只有一個項目和一個倉庫:

+--------+-------------+------------+-----------------+----------+ 
| ItemID | WarehouseID | CreateDate | PhysicalQtyRule | Quantity | Balance | comments 
+--------+-------------+------------+-----------------+----------+ 
| 1234 |   11 | 2013-03-25 |    2 |  35 |  35 | Rule 2 = In 
| 1234 |   11 | 2013-03-28 |    3 |  30 |  5 | Rule 3 = Out 
| 1234 |   11 | 2013-04-01 |    1 |  3 |  3 | Rule 1 = Reset 
| 1234 |   11 | 2013-07-12 |    2 |  40 |  43 | Rule 2 = In 
| 1234 |   11 | 2013-09-05 |    3 |  20 |  23 | Rule 3 = Out 
| 1234 |   11 | 2013-12-31 |    1 |  25 |  25 | Rule 1 = Reset 
| 1234 |   11 | 2014-01-09 |    3 |  11 |  14 | Rule 3 = Out 
| 1234 |   11 | 2014-01-16 |    3 |  6 |  8 | Rule 3 = Out 

我想知道的任何變量日期的平衡。

+0

您是否試圖僅僅因爲所有這個表創建而得到最終查詢,或者您是否需要主動存儲所有數據? – TTeeple

+0

只需要最終查詢。通過所有這些步驟來嘗試最好的解釋。 – PJLG

+1

您能否提供一些樣本數據和預期結果? – TTeeple

回答

0

沒有你的數據,我無法測試這個,但我相信這應該是你的解決方案。

SELECT i.ItemID 
    ,w.WarehouseID 
    ,[Qty] = tbl2.Quantity + tbl3.Quantity – tbl4.Quantity 
FROM Item i 
CROSS JOIN Warehouse w 
OUTER APPLY (
SELECT [DATE1] = MAX(sh.CreateDate) 
     ,sh.ItemID 
     ,sh.Quantity 
     ,sh.WarehouseID 
FROM StockHistory sh 
WHERE sh.PhysicalQtyRule = 1 AND sh.CreateDate <= @datevar 
AND i.ItemID = sh.ItemID 
    AND w.WarehouseID = sh.WarehouseID 
GROUP BY sh.ItemID, sh.Quantity, sh.WarehouseID) tbl2 
OUTER APPLY (
SELECT sh.ItemID 
    ,sh.WarehouseID 
    ,[Quantity] = SUM(sh.Quantity) 
FROM StockHistory sh 
WHERE sh.PhysicalQtyRule = 2 AND sh.CreateDate > tbl2.DATE1 --If exists 
AND sh.CreateDate <= @datevar AND i.ItemID = sh.ItemID 
AND w.WarehouseID = sh.WarehouseID 
GROUP BY sh.ItemID, sh.WarehouseID) tbl3 
OUTER APPLY (
SELECT sh.ItemID 
    ,sh.WarehouseID 
    ,[Quantity] = SUM(sh.Quantity) 
FROM StockHistory sh 
WHERE sh.PhysicalQtyRule = 3 AND sh.CreateDate > tbl2.DATE1 --If exists 
AND sh.CreateDate <= @datevar AND i.ItemID = sh.ItemID 
    AND w.WarehouseID = sh.WarehouseID 
GROUP BY sh.ItemID, sh.WarehouseID) tbl4 
+0

我不知道爲什麼,但查詢沒有識別tbl2.DATE1(兩者都接近「If exists」)和別名tbl3和tbl4 ... – PJLG

+0

而倉庫表沒有ItemID字段。我的第一個查詢是「完美」(我認爲),因爲我想要所有Items x所有倉庫,但我也可以從這個查詢中得到它:'SELECT ItemID,WarehouseID FROM StockHistory GROUP BY ItemID,WarehouseID' – PJLG

+0

@PJLG您可以取出'CROSS JOIN'的'ON'子句。它與你的查詢是相同的執行計劃,只是一個更易讀的IMO。仍在研究其他問題。 – TTeeple