do rounding

This commit is contained in:
Paul Trowbridge 2022-04-12 02:30:26 -04:00
parent d5c2980bb1
commit 95675428a4
2 changed files with 46 additions and 23 deletions

View File

@ -99,9 +99,9 @@ SELECT
--create the column reference
CASE appcol
----calculation should yield the value necessary to get to the target increment value------------
WHEN 'units' THEN 'o.'||format('%I',cname)||' * vscale.factor AS '||format('%I',cname)
WHEN 'value' THEN 'o.'||format('%I',cname)||' * vscale.factor AS '||format('%I',cname)
WHEN 'cost' THEN 'o.'||format('%I',cname)||' * vscale.factor AS '||format('%I',cname)
WHEN 'units' THEN 'round(o.'||format('%I',cname)||' * vscale.factor,5) AS '||format('%I',cname)
WHEN 'value' THEN 'round(o.'||format('%I',cname)||' * vscale.factor,2) AS '||format('%I',cname)
WHEN 'cost' THEN 'round(o.'||format('%I',cname)||' * vscale.factor,2) AS '||format('%I',cname)
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
WHEN 'iter' THEN '''scale volume'' AS '||format('%I',_iter_col)
WHEN 'logid' THEN '(SELECT id FROM ilog) AS '||format('%I',_logid_col)
@ -126,9 +126,9 @@ SELECT
CASE appcol
WHEN 'units' THEN '0::numeric AS '||format('%I',cname)
WHEN 'cost' THEN '0::numeric AS '||format('%I',cname)
WHEN 'value' THEN $$(CASE WHEN pscale.factor = 0 THEN o.$$||_units_col||$$ * pscale.mod_price ELSE o.$$||format('%I',cname)||' * pscale.factor END)::numeric AS '||format('%I',cname)
WHEN 'value' THEN $$round((CASE WHEN pscale.factor = 0 THEN o.$$||_units_col||$$ * pscale.mod_price ELSE o.$$||format('%I',cname)||' * pscale.factor END)::numeric,2) AS '||format('%I',cname)
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
WHEN 'iter' THEN '''scale volume'' AS '||format('%I',_iter_col)
WHEN 'iter' THEN '''scale price'' AS '||format('%I',_iter_col)
WHEN 'logid' THEN '(SELECT id FROM ilog) AS '||format('%I',_logid_col)
ELSE 'o.'||format('%I',cname)
END
@ -205,7 +205,7 @@ WHERE
,volume AS (
SELECT * FROM volume_only
UNION ALL
SELECT * FROM baseline WHERE (SELECT COUNT(*) FROM volume_only) = 0
SELECT * FROM basemix WHERE (SELECT factor FROM vscale) = 0
)
,pscale AS (
SELECT
@ -215,10 +215,13 @@ SELECT
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
0
ELSE
--expects inbound partial adjustment from volume table
--if the volume table does not have enough price added, need to get to goal increment
CASE WHEN (SELECT factor FROM vscale) = 0
--if the incoming volume dataset has just basemix data, then create an absolute factor and multiply by basemix
THEN (SELECT pincr FROM target)/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
--if the incoming volume table already has an adjusment, just need to create a true-up factor
--multiplying by the raw factor without adding 1 will work since this data set will be added to the volume data set
((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
ELSE ((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
END
END factor
,CASE WHEN (SELECT sum($$||format('%I',_value_col)||$$) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||format('%I',_value_col)||$$) FROM volume)) <> 0 THEN

View File

@ -71,14 +71,14 @@ SELECT
,o."Trans. Date"
,o."Post Date"
,o."Description"
,o."Amount" * vscale.factor AS "Amount"
,round(o."Amount" * vscale.factor,2) AS "Amount"
,o."Category"
,o."Party"
,o."Reason"
,'app_forecast_name' AS app_version
,'scale volume' AS app_iter
,(SELECT id FROM ilog) AS app_logid
,o.app_units * vscale.factor AS app_units
,round(o.app_units * vscale.factor,5) AS app_units
FROM
basemix o
CROSS JOIN vscale
@ -88,15 +88,35 @@ WHERE
,volume AS (
SELECT * FROM volume_only
UNION ALL
SELECT * FROM baseline WHERE (SELECT COUNT(*) FROM volume_only) = 0
SELECT * FROM basemix WHERE (SELECT factor FROM vscale) = 0
)
,pscale AS (
----what if the selection has several value columns, which one to use?-----
SELECT
SELECT
(SELECT pincr FROM target) AS target_increment
,sum("Amount") AS value
,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN 0 ELSE (SELECT pincr FROM target)/sum("Amount") FROM volume END factor
FROM
,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
0
ELSE
CASE WHEN (SELECT factor FROM vscale) = 0
--if the incoming volume dataset has just basemix data, then create an absolute factor and multiply by basemix
THEN (SELECT pincr FROM target)/(SELECT sum("Amount") FROM volume)
--if the incoming volume table already has an adjusment, just need to create a true-up factor
--multiplying by the raw factor without adding 1 will work since this data set will be added to the volume data set
ELSE ((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") FROM volume)
END
END factor
,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") 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("Amount") FROM volume))/(SELECT sum(app_units) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
FROM
volume
)
,pricing AS (
@ -106,12 +126,12 @@ SELECT
,o."Trans. Date"
,o."Post Date"
,o."Description"
,(CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric AS "Amount"
,round((CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric,2) AS "Amount"
,o."Category"
,o."Party"
,o."Reason"
,'app_forecast_name' AS app_version
,'scale volume' AS app_iter
,'scale price' AS app_iter
,(SELECT id FROM ilog) AS app_logid
,0::numeric AS app_units
FROM
@ -125,7 +145,7 @@ INSERT INTO
SELECT
*
FROM
volume
volume_only
UNION ALL
SELECT
*