Calculating the Weighted Average Cost of products stock

前端 未结 5 1555
甜味超标
甜味超标 2020-12-28 23:12

I have to calculate my products stock cost, so for every product after each buy, i have to recalculate the Weighted Average Cost.

I got a view thats

5条回答
  •  滥情空心
    2020-12-28 23:57

    I have spent several hours on this moving average! Mainly because of the not reliable window function first/last/nth_value, according to Postgresql documentation on window functions: This is likely to give unhelpful results for nth_value and particularly last_value.

    The answer is incomplete. Things to do:

    • make WAC stay the same value when qty_in = 0 (now it is zeroed out)
    • WAC is not calculated based on previous WAC but on "previous" price, that's why there are increasing differences between WAC calculated by me and presented by You

    SQLFiddle

提交回复
热议问题