rework addmonth routes to accomodate -0- in the base

This commit is contained in:
fleetside72 2019-04-03 04:27:57 -04:00
parent 723e2a250c
commit 752e65de05
2 changed files with 102 additions and 9 deletions

View File

@ -11,7 +11,10 @@ target AS (select target_increment incr)
SELECT SELECT
sum(fb_qty) tot sum(fb_qty) tot
,sum(fb_qty) FILTER (WHERE iter = 'copy') base ,sum(fb_qty) FILTER (WHERE iter = 'copy') base
,sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket') newpart ,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart
,sum(fb_val_loc *r_rate) totsales
,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales
FROM FROM
rlarp.osm_fcpool rlarp.osm_fcpool
WHERE WHERE
@ -40,6 +43,19 @@ target AS (select target_increment incr)
ELSE ELSE
'scale all' 'scale all'
END flag END flag
,CASE WHEN totsales = 0 THEN
CASE WHEN basesales = 0 THEN
CASE WHEN newpartsales = 0 THEN
'no price'
ELSE
'scale new part'
END
ELSE
'scale copy'
END
ELSE
'scale all'
END flagsales
FROM FROM
testv testv
) )
@ -92,7 +108,7 @@ target AS (select target_increment incr)
,orderdate ,orderdate
,requestdate ,requestdate
,shipdate ,shipdate
,sum(value_usd) value_usd ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM FROM
rlarp.osm_fcpool rlarp.osm_fcpool
LEFT OUTER JOIN mseq ON LEFT OUTER JOIN mseq ON
@ -118,7 +134,7 @@ target AS (select target_increment incr)
,requestdate ,requestdate
,shipdate ,shipdate
HAVING HAVING
sum(value_usd) <> 0 sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
) )
--select * from alldates --select * from alldates
,dom AS ( ,dom AS (

View File

@ -3,6 +3,58 @@ WITH
the volume must be expressed in terms of units, since that is what it will be scaling the volume must be expressed in terms of units, since that is what it will be scaling
*/ */
target AS (select target_volume vincr, target_price pincr) target AS (select target_volume vincr, target_price pincr)
,testv AS (
SELECT
sum(fb_qty) tot
,sum(fb_qty) FILTER (WHERE iter = 'copy') base
,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart
,sum(fb_val_loc *r_rate) totsales
,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales
FROM
rlarp.osm_fcpool
WHERE
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate
)
,flagv AS (
SELECT
tot
,base
,newpart
,CASE WHEN tot = 0 THEN
CASE WHEN base = 0 THEN
CASE WHEN newpart = 0 THEN
'unclean data. tested -> does not exist'
ELSE
'scale new part'
END
ELSE
'scale copy'
END
ELSE
'scale all'
END flag
,CASE WHEN totsales = 0 THEN
CASE WHEN basesales = 0 THEN
CASE WHEN newpartsales = 0 THEN
'no price'
ELSE
'scale new part'
END
ELSE
'scale copy'
END
ELSE
'scale all'
END flagsales
FROM
testv
)
,GLD AS ( ,GLD AS (
SELECT SELECT
N1COMP COMP N1COMP COMP
@ -52,7 +104,7 @@ target AS (select target_volume vincr, target_price pincr)
,orderdate ,orderdate
,requestdate ,requestdate
,shipdate ,shipdate
,sum(value_usd) value_usd ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM FROM
rlarp.osm_fcpool rlarp.osm_fcpool
LEFT OUTER JOIN mseq ON LEFT OUTER JOIN mseq ON
@ -61,6 +113,11 @@ target AS (select target_volume vincr, target_price pincr)
-----------------scenario---------------------------- -----------------scenario----------------------------
where_clause where_clause
-----------------additional params------------------- -----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
@ -72,7 +129,7 @@ target AS (select target_volume vincr, target_price pincr)
,requestdate ,requestdate
,shipdate ,shipdate
HAVING HAVING
sum(value_usd) <> 0 sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
) )
--select * from alldates --select * from alldates
,dom AS ( ,dom AS (
@ -245,6 +302,11 @@ SELECT
-----------------scenario---------------------------- -----------------scenario----------------------------
where_clause where_clause
-----------------additional params------------------- -----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN iterdef->>'type' = 'new basket'
END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND adj_orderdate <= adj_shipdate
GROUP BY GROUP BY
@ -445,8 +507,21 @@ WHERE
SELECT SELECT
(SELECT pincr::numeric FROM target) incr (SELECT pincr::numeric FROM target) incr
,(SELECT sum(fb_val_loc * r_rate) FROM volume) base ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base
--(target_sales - current_state)/current state = adjustment factor ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN
,((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume) factor 0
ELSE
((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume)
END factor
,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume)) <> 0 THEN
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply
((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_qty) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
) )
,price AS ( ,price AS (
SELECT SELECT
@ -552,14 +627,16 @@ SELECT
,b.mod_chansub ,b.mod_chansub
,b.quota_rep_descr ,b.quota_rep_descr
,b.director_descr ,b.director_descr
,b.fb_val_loc*p.factor value_loc ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price/b.r_rate ELSE b.fb_val_loc*p.factor END)::numeric value_loc
,b.fb_val_loc*p.factor*r_rate value_usd ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price ELSE b.fb_val_loc*p.factor END)::numeric value_usd
,0 cost_loc ,0 cost_loc
,0 cost_usd ,0 cost_usd
,0 units ,0 units
FROM FROM
volume b volume b
CROSS JOIN pscale p CROSS JOIN pscale p
WHERE
p.factor <> 0 or p.mod_price <> 0
) )
, ins AS ( , ins AS (
INSERT INTO rlarp.osm_fcpool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING * INSERT INTO rlarp.osm_fcpool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING *