pivot_forecast/routes/scale/scale.sql

127 lines
2.9 KiB
MySQL
Raw Normal View History

WITH
req AS (SELECT $$app_req$$::jsonb j)
,target AS (
SELECT
2022-04-09 02:29:51 -04:00
(req.j->>'app_vincr')::numeric vincr --volume
,(req.j->>'app_pincr')::numeric pincr --price
FROM
req
)
-----this block is supposed to test for new products that might not be in baseline etc-------
2020-12-05 01:19:17 -05:00
,test AS (
SELECT
2022-04-09 02:29:51 -04:00
sum(app_units) FILTER (WHERE version <> 'ACTUALS') total
,sum(app_units) FILTER (WHERE iter = 'baseline') base
FROM
2022-04-09 02:29:51 -04:00
tpsv.dcard o
WHERE
app_where
)
,basemix AS (
SELECT
2022-04-09 02:29:51 -04:00
o.id
,o.logid
,o."Trans. Date"
,o."Post Date"
,o."Description"
,o."Amount"
,o."Category"
,o."Party"
,o."Reason"
,o.app_units
2020-12-05 01:19:17 -05:00
FROM
2022-04-09 02:29:51 -04:00
tpsv.dcard o
WHERE
2020-12-05 01:19:17 -05:00
app_where
),
vscale AS (
SELECT
(SELECT vincr FROM target) AS target_increment
2022-04-09 02:29:51 -04:00
,sum(app_units) AS units
,CASE WHEN sum(app_units) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum(app_units) END AS factor
2020-12-05 01:19:17 -05:00
FROM
basemix
)
2022-04-09 02:29:51 -04:00
,ilog AS (
INSERT INTO
fc.log (doc)
SELECT
req.j
FROM
req
)
,volume AS (
SELECT
2022-04-09 02:29:51 -04:00
o.id
,o.logid
,o."Trans. Date"
,o."Post Date"
,o."Description"
,o."Amount" * vscale.factor AS "Amount"
,o."Category"
,o."Party"
,o."Reason"
,o.app_units * vscale.factor AS app_units
,'app_forecast_name' AS version
2020-12-08 01:13:31 -05:00
,'scale vol' AS iter
FROM
2020-12-05 01:19:17 -05:00
basemix o
CROSS JOIN vscale
)
,pscale AS (
SELECT
(SELECT pincr FROM target) AS target_increment
2022-04-09 02:29:51 -04:00
,sum("Amount") AS value
,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
--if the target dollar value still does not match the target increment, make this adjustment
2022-04-09 02:29:51 -04:00
((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") FROM volume)
END factor
2022-04-09 02:29:51 -04:00
,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
2022-04-09 02:29:51 -04:00
((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") FROM volume))/(SELECT sum(app_units) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
2020-12-05 01:19:17 -05:00
FROM
volume
)
,pricing AS (
SELECT
2022-04-09 02:29:51 -04:00
o.id
,o.logid
,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
,o."Category"
,o."Party"
,o."Reason"
,0::numeric
2022-04-09 02:29:51 -04:00
,'app_forecast_name' AS version
2020-12-08 01:13:31 -05:00
,'scale price' AS iter
FROM
volume o
CROSS JOIN pscale
WHERE
pscale.factor <> 0 or pscale.mod_price <> 0
)
INSERT INTO
2022-04-09 02:29:51 -04:00
tpsv.dcard o
2020-12-08 01:13:31 -05:00
SELECT
*
FROM
volume
UNION ALL
SELECT
*
FROM
pricing