3

私は2つのテーブルを持っています。1 つは特定の単位の数量を保持します。もう 1 つは、これらのユニットが過去に到着したときの数量、日付、およびコストを保持します。

最初のテーブルでは、UNIT "ABC" の数量は 50 です。

2 番目のテーブルには、次のようなデータがあります。

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 を使用してデータをマイニングしています。したがって、これらのプラットフォームのいずれかに関連するソリューションは素晴らしいものです。

4

1 に答える 1

2

これ:-

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
于 2013-11-15T02:08:34.083 に答える