【问题标题】:Calculating inventory cost in SQLSQL计算库存成本
【发布时间】:2013-11-14 22:06:13
【问题描述】:

我有两张桌子。一个持有特定单位的数量。另一个保存这些单位历史上到达时的数量、日期和成本。

所以在我的第一个表中,我会说 UNIT "ABC" 的数量为 50

在第二个表中,我的数据如下所示:

Unit   Date arrived   Quantity   Cost
----   ------------   --------   -----
ABC       11/1            100    $3.00
ABC       11/4             15    $5.00
ABC       11/5             25    $6.00

所以在本例中,我需要根据先进先出系统计算 50 件商品的价值。 50 个项目的数学运算如下所示:

25 x $6.00
15 x $5.00
10 x $3.00

该商品的总价值为 255.00 美元

所以我需要处理大约 300,000 个项目,并且我需要一个“简单按钮”。目前使用 MS Access & SQL 来挖掘我的数据。因此,与这些平台中的任何一个相关的任何解决方案都会很棒。

【问题讨论】:

  • 如果您想要 50 个“ABC”项目的 FIFO 成本,根据第二个表中的数据,您将从第一行获得 3.00 美元。如果您有 110 个“ABC”项目,则 FIFO 单位成本将为 350/110 美元。这是你需要的吗?您要求“先进先出”,但您只有“进”。
  • 我弄清楚了 FIFO 在这种情况下的含义(我认为)。实际上,库存是按日期顺序 (FI) 装载的,但假设是货架上剩下的任何东西都在那里,因为任何被拉出的物品 - 都会从最早交货 (FO) 中拉出。因此,持有的任何股票的价值都将根据最近交货的最新价格计算出来——然后向后计算,直到库存的价值被估价。明明很久没写存货控制系统了……
  • @dav1dsm1th 是的,这更有意义,并且符合发布的详细信息。我想这表明自从我开始计算库存成本以来已经有更长的时间了,哈哈
  • 您是否正在计算当前价值,并随着库存水平的变化?如果是这样,是否按批次跟踪项目?如果是和否,也许可以假设第一个将是第一个被删除的。如果 YES 和 YES,您的方法将变得更加复杂。您的帖子没有提出这些问题,但它们是库存估值的典型问题。

标签: sql ms-access


【解决方案1】:

这个:-

select s.unit, dv.cost_2d+((s.quantity-dv.quantity_2d)*dv.cost) as valuation
from (
    select 
        d.*, 
        isnull((
            select sum(csq.quantity) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) as quantity_2d,
        isnull((
            select sum(csq.quantity*csq.cost) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) as cost_2d
    from #delivery d

    -- possible optimization - reduces the number of rows
    -- if we have enough to calculate value of stock held

    --join #stock s on s.unit=d.unit
    --and isnull((
    --    select sum(csq.quantity)
    --    from #delivery csq
    --    where csq.unit=d.unit 
    --        and csq.arrived>d.arrived
    --    ),0)<=s.quantity 

    -- you'd need to test if it helps/hinders with your dataset/schema

) as dv
join #stock s on s.unit=dv.unit
where dv.quantity+dv.quantity_2d>=s.quantity 
    and dv.quantity_2d<s.quantity

产生:-

unit    valuation
abc     255.00

如果来自:-

create table #stock (
    unit varchar(10),
    quantity int
)

create table #delivery (
    unit varchar(10),
    arrived date,
    quantity int,
    cost money
)

insert into #stock values ('abc',50)
insert into #delivery values ('abc','2013-11-01',100,3)
insert into #delivery values ('abc','2013-11-04',15,5)
insert into #delivery values ('abc','2013-11-05',25,6)

-----------更新-----------------------------------

这是另一个可能会或可能不会运行得更快的版本 - 取决于您的数据集/架构:-

select dv.unit, dv.cost_2d+((dv.instock-dv.quantity_2d)*dv.cost) as valuation
from (
    select 
        d.*, 
        isnull((
            select sum(csq.quantity) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) as quantity_2d,
        isnull((
            select sum(csq.quantity*csq.cost) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) as cost_2d,
        s.quantity as instock
    from #delivery d
    join #stock s on s.unit=d.unit
    and s.quantity between isnull((
            select sum(csq.quantity) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) and isnull((
            select sum(csq.quantity) 
            from #delivery csq 
            where csq.unit=d.unit 
                and csq.arrived>d.arrived
        ),0) + d.quantity
) as dv

【讨论】:

  • 我在答案的底部添加了第二个优化。这会产生相同的结果,并且可以让查询在 OPs 环境中更有效地运行(我的测试数据太小,无法衡量代码的任何优势/劣势)。
  • 应该能够对此进行改进并在派生表中计算出估值 - 进一步简化代码(如果 OP 有性能问题,我可能会回到这个问题)。
  • 您的第二组代码运行完美(而且速度很快)。太棒了。
  • 很高兴听到这个消息。 d.* 应该真正扩展到所需的字段列表 - 为优化器提供使代码尽可能高效运行的最佳机会(你不应该在生产代码中使用 * —— 除了在 EXISTS 中) .一些适当的索引永远不会造成任何伤害......
  • 这回答了你的问题吗?
猜你喜欢
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
相关资源
最近更新 更多