多个sql查询还是Cursor?

时间:2014-02-17 19:08:29

标签: sql sql-server

我需要帮助处理对我来说似乎很复杂的事情。

我创建了一个查询来创建一个tbl1,它是表Item和Warehouse的笛卡尔积。它让我回到所有仓库中的所有物品:

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

我做了第二个查询(tbl2),在那里我检查上一个或等于变量日期(@datevar)的最后一个文档的日期,其数量规则是1(PhysicalQtyRule = 1),这是通过Item和Warehouse获得的来自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 
    
  4. 我不知道是否需要游标(对我而言是新的)或多个查询,但重要的是最佳性能,因为我的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
    

    我想知道任何变量日期的余额。

1 个答案:

答案 0 :(得分: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