Commit b036965a authored by Klaus Wölfel's avatar Klaus Wölfel

WIP: Add lowest_value_test to WeightedAverage

parent 285343a1
...@@ -7,13 +7,16 @@ since they rely on the same kind of "split by period" model. ...@@ -7,13 +7,16 @@ since they rely on the same kind of "split by period" model.
First split timeframe into Year (or Month) periods, and fold all movements First split timeframe into Year (or Month) periods, and fold all movements
during each period. during each period.
Then perform a weighted average over all periods. Then perform a weighted average over all periods.
Parameter lowest_value_test compares latest price and WeightedAverage average price and takes the
smallest one.
*/ */
set @total_asset_price=0.0, @total_quantity=0.0 set @total_asset_price=0.0, @total_quantity=0.0
<dtml-var sql_delimiter> <dtml-var sql_delimiter>
select select
byperiod.*, byperiod.*,
(@unit_price:=(@total_asset_price+incoming_total_price)/(@total_quantity+incoming_total_quantity)) as unit_price, (@unit_price:=MIN((@total_asset_price+incoming_total_price)/(@total_quantity+incoming_total_quantity)), last_price) as unit_price,
(@total_asset_price:= (@total_asset_price:=
@total_asset_price + @total_asset_price +
incoming_total_price + incoming_total_price +
...@@ -27,11 +30,21 @@ from ...@@ -27,11 +30,21 @@ from
SUM(IF(quantity>0, total_price, 0)) as incoming_total_price, SUM(IF(quantity>0, total_price, 0)) as incoming_total_price,
SUM(IF(quantity>0, quantity, 0)) as incoming_total_quantity, SUM(IF(quantity>0, quantity, 0)) as incoming_total_quantity,
SUM(IF(quantity>0, 0, quantity)) as outgoing_total_quantity, SUM(IF(quantity>0, 0, quantity)) as outgoing_total_quantity,
SUM(quantity) as quantity_diff SUM(quantity) as quantity_diff,
MAX(last_price) as last_price
from from
stock, catalog (
where select
<dtml-var where_expression> date,
total_price,
quantity,
IF(date=MAX(IF(quantity>0, date, 0)) OVER (PARTITION BY year(date)), total_price, 0) as last_price
<dtml-if "'Monthly' in valuation_method">, IF(date=MAX(IF(quantity>0, date, 0)) OVER (PARTITION BY month(date)), total_price, 0) as last_price</dtml-if>
from
stock, catalog
where
<dtml-var where_expression>
) as last_price_per_period
group by group by
d_year<dtml-if "'Monthly' in valuation_method">, d_month</dtml-if> d_year<dtml-if "'Monthly' in valuation_method">, d_month</dtml-if>
order by order by
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment