user2994049
user2994049

Reputation: 33

Calculating inventory cost in SQL

I have two tables. One holds the quantity for a specific unit. The other holds the quantities, dates and costs when these units historically arrived.

So in my first table, I would say UNIT "ABC" has a quantity of 50

In the second table, I would have data that looks like this:

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

So in this example, I would need to figure the value of the 50 items based on a first in, first out system. The math would look like this for the 50 items:

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

The total value for this item would be $255.00

So I need to do this with about 300,000 items and I need an "easy button". Currently using MS Access & SQL to mine my data. So any solution relevant to either of those platforms would be great.

Upvotes: 3

Views: 2236

Answers (1)

db9dreamer
db9dreamer

Reputation: 1715

This:-

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

produces:-

unit    valuation
abc     255.00

if fed from:-

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)

-----------UPDATE-----------------------------------

Here's another version that may or may not run quicker - depending on your dataset/schema:-

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

Upvotes: 2

Related Questions