Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 动态分母_Sql_Sql Server - Fatal编程技术网

Sql 动态分母

Sql 动态分母,sql,sql-server,Sql,Sql Server,由于没有更好的术语,我正在寻找一种动态改变分母的方法,以更好地解释相对于理想库存的库存水平的动态变化 我无法很好地表达这一点,所以举个例子或许会有所帮助 比如说一家公司持有一份特定的存货。该库存可能因销售而波动,有时可能出现供应过剩。该库存通常根据理想库存水平进行检查,与相对于整体价值的理想模型相比,当这些库存低于一定水平时,将从供应商处采购零件。即,如果当前库存持有第1部分的3%,但理想库存表明我们应持有第1部分的5%,我们知道我们需要购买更多的第1部分 非常简单,除非供应商无法提供更多零件。

由于没有更好的术语,我正在寻找一种动态改变分母的方法,以更好地解释相对于理想库存的库存水平的动态变化

我无法很好地表达这一点,所以举个例子或许会有所帮助

比如说一家公司持有一份特定的存货。该库存可能因销售而波动,有时可能出现供应过剩。该库存通常根据理想库存水平进行检查,与相对于整体价值的理想模型相比,当这些库存低于一定水平时,将从供应商处采购零件。即,如果当前库存持有第1部分的3%,但理想库存表明我们应持有第1部分的5%,我们知道我们需要购买更多的第1部分

非常简单,除非供应商无法提供更多零件。因此,我们当前的库存包含不同数量的零件1-10(理想库存也是如此)。然而,我们目前库存中的两个零件我们需要更多,但供应商不能再供应了

因为我们不能再订购这些零件,为了计算其他零件的相对库存,我们需要调整缺货零件的价值,使其与理想库存一致。让我们假设我们这样做是通过以美元为基础,将理想库存水平(例如5%)与库存中所有项目的总价值相比较。然后,我们计算每个零件的理想库存水平与当前库存水平之间的差值,并将该值添加到缺货项目的现有库存(每个零件)中。该最终值为我们提供了库存中所有项目的调整后总价值,用于确定优先采购的库存项目(对于供应商库存的项目)

如下所示,问题在于,根据缺货项目进行的理论调整是基于根据初始库存价值而不是调整后的库存价值计算的分母。因此,将总重量调整为与理想库存水平一致的缺货项目关闭

是否有一种方法可以动态地说明最终导致缺货项目相对重量的调整,这些缺货项目符合理想库存

DECLARE @Inventory TABLE
    (PartID VARCHAR(10),
    Quantity FLOAT
    )

DECLARE @JITInventory TABLE
    (PartID VARCHAR(10),
    Quantity FLOAT,
    isOutOfStock BIT
    )

DECLARE @Price TABLE
    (PartID VARCHAR(10),
    Price FLOAT)

Insert Into @Inventory VALUES
    ('Part 1','10'),
    ('Part 2','15'),
    ('Part 3','10'),
    ('Part 4','12'),
    ('Part 5','9'),
    ('Part 6','8'),
    ('Part 7','6'),
    ('Part 8','13'),
    ('Part 9','14'),
    ('Part 10','18')

Insert Into @JITInventory VALUES
    ('Part 1','15000',1),
    ('Part 2','18067',0),
    ('Part 3','6776',0),
    ('Part 4','10000',0),
    ('Part 5','14455',1),
    ('Part 6','4517',0),
    ('Part 7','22586',0),
    ('Part 8','6776',0),
    ('Part 9','994',0),
    ('Part 10','1355',0)

Insert Into @Price VALUES
    ('Part 1','10'),
    ('Part 2','15'),
    ('Part 3','10'),
    ('Part 4','12'),
    ('Part 5','9'),
    ('Part 6','8'),
    ('Part 7','6'),
    ('Part 8','13'),
    ('Part 9','14'),
    ('Part 10','18')        
    ;


--ideal inventory level as percentage of total
SELECT
    jit.partid,
    jit.Quantity,
    p.Price,
    Net=Quantity*Price,
    Wt=(Quantity*Price)/
        SUM(Quantity*Price) OVER(Partition By 1),
    isOutOfStock
Into #JITInv
FROM @JITInventory jit
    join @Price p
    on jit.PartID=p.PartID


--current inventory level
SELECT
    I.partid,
    I.Quantity,
    p.Price,
    Net=Quantity*Price,
    Wt=(Quantity*Price)/
        SUM(Quantity*Price) OVER(Partition By 1)
Into #Inv
FROM @Inventory I
    join @Price p
    on I.PartID=p.PartID

--knowing we can't order more out of stock items from 
--the supplier, adjust the items we can't control 
--to be in line with our ideal inventory by 
--temporarily assuming we're purchasing those items
--to be held at the same weight as the ideal inventory.
--  i.e. take the weight of the parts in the ideal
--  inventory and multiply by the total value
--  of the current inventory to get
--  the value of the part IDs we assume are 
--  in stock so that the weights of other parts
--  aren't impacted.
SELECT
    J.PartID,
    J.Wt*(Select SUM(NET) FROM #Inv) as JITValue,
    I.Net as IValue,
    J.Wt*(Select SUM(NET) FROM #Inv)-I.Net as Adjustment
INTO #InvAdjust
FROM #Inv I
    JOIN #JITInv J
    ON I.PartID=J.PartID
WHERE isOutOfStock=1
以及我们当前库存中供应商缺货的项目与基于理想库存的调整值的比较结果:

PartID  JITValue          IValue    Adjustment
Part 1  208.167374863295    100       108.167374863295
Part 5  180.543564218936    81        99.5435642189359  
然后重新计算所有头寸的价值,假设我们已经购买了缺货项目,以创建一个基线,在该基线中,供应商的剩余库存项目可以优先购买

--Now using the adjusted value to be in line with the ideal (JIT) model,
--determine the new parts weights to determine how we should
--prioritize buying/selling items that aren't currently out of stock.

--Issue here is because the adjustment of Parts 1 and 5 are relative
--to the current value of the inventory, the weights are once again
--skewed from the ideal inventory level.
SELECT
    I.PartID,
    I.Net CurrentNet,
    I.Net+COALESCE(IA.Adjustment,0) as NewNet,
    (I.Net+COALESCE(IA.Adjustment,0))
        /SUM(I.Net+COALESCE(IA.Adjustment,0)) OVER(Partition By 1) AS NewWt,
    J.Wt as JITWt

FROM #Inv I
  LEFT JOIN #InvAdjust IA
   ON I.PartID=IA.PartID
  JOIN #JITInv J
   ON J.PartID=I.PartID