2020-12-05 00:10:47 -05:00
|
|
|
WITH
|
2020-12-08 00:49:04 -05:00
|
|
|
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
|
2020-12-08 00:49:04 -05:00
|
|
|
FROM
|
|
|
|
req
|
|
|
|
)
|
2020-12-05 00:10:47 -05:00
|
|
|
-----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 (
|
2020-12-05 00:10:47 -05:00
|
|
|
SELECT
|
2022-04-12 01:58:52 -04:00
|
|
|
sum(app_units) FILTER (WHERE app_iter <> 'ACTUALS') total
|
|
|
|
,sum(app_units) FILTER (WHERE app_iter = 'baseline') base
|
2020-12-05 00:10:47 -05:00
|
|
|
FROM
|
2022-04-09 02:29:51 -04:00
|
|
|
tpsv.dcard o
|
2020-12-05 00:10:47 -05:00
|
|
|
WHERE
|
|
|
|
app_where
|
|
|
|
)
|
2022-04-12 01:58:52 -04:00
|
|
|
,ilog AS (
|
|
|
|
INSERT INTO
|
|
|
|
fc.log (doc)
|
|
|
|
SELECT
|
|
|
|
req.j
|
|
|
|
FROM
|
|
|
|
req
|
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
------need to group basemix and forego any detail iterations
|
2020-12-05 00:10:47 -05:00
|
|
|
,basemix AS (
|
|
|
|
SELECT
|
2022-04-09 02:29:51 -04:00
|
|
|
o.id
|
|
|
|
,o.logid
|
|
|
|
,o."Trans. Date"
|
|
|
|
,o."Post Date"
|
|
|
|
,o."Description"
|
2022-04-12 01:58:52 -04:00
|
|
|
,sum(o."Amount") AS "Amount"
|
2022-04-09 02:29:51 -04:00
|
|
|
,o."Category"
|
|
|
|
,o."Party"
|
|
|
|
,o."Reason"
|
2022-04-22 00:22:35 -04:00
|
|
|
,sum(o.app_units) AS app_units
|
2022-04-12 01:58:52 -04:00
|
|
|
,'app_forecast_name' AS app_version
|
|
|
|
,'scale' AS app_iter
|
|
|
|
,0::bigint AS app_logid
|
2020-12-05 01:19:17 -05:00
|
|
|
FROM
|
2022-04-09 02:29:51 -04:00
|
|
|
tpsv.dcard o
|
2020-12-05 00:10:47 -05:00
|
|
|
WHERE
|
2020-12-05 01:19:17 -05:00
|
|
|
app_where
|
2022-04-12 01:58:52 -04:00
|
|
|
GROUP BY
|
|
|
|
o.id
|
|
|
|
,o.logid
|
|
|
|
,o."Trans. Date"
|
|
|
|
,o."Post Date"
|
|
|
|
,o."Description"
|
|
|
|
,o."Category"
|
|
|
|
,o."Party"
|
|
|
|
,o."Reason"
|
2020-12-05 00:10:47 -05:00
|
|
|
),
|
|
|
|
vscale AS (
|
|
|
|
SELECT
|
2020-12-08 00:49:04 -05:00
|
|
|
(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
|
2020-12-05 00:10:47 -05:00
|
|
|
)
|
2022-04-12 01:58:52 -04:00
|
|
|
,volume_only AS (
|
2020-12-05 00:10:47 -05:00
|
|
|
SELECT
|
2022-04-09 02:29:51 -04:00
|
|
|
o.id
|
|
|
|
,o.logid
|
|
|
|
,o."Trans. Date"
|
|
|
|
,o."Post Date"
|
|
|
|
,o."Description"
|
2022-04-12 02:30:26 -04:00
|
|
|
,round(o."Amount" * vscale.factor,2) AS "Amount"
|
2022-04-09 02:29:51 -04:00
|
|
|
,o."Category"
|
|
|
|
,o."Party"
|
|
|
|
,o."Reason"
|
2022-04-22 00:22:35 -04:00
|
|
|
,round(o.app_units * vscale.factor,5) AS app_units
|
2022-04-12 01:58:52 -04:00
|
|
|
,'app_forecast_name' AS app_version
|
|
|
|
,'scale volume' AS app_iter
|
|
|
|
,(SELECT id FROM ilog) AS app_logid
|
2020-12-05 00:10:47 -05:00
|
|
|
FROM
|
2020-12-05 01:19:17 -05:00
|
|
|
basemix o
|
2020-12-05 00:10:47 -05:00
|
|
|
CROSS JOIN vscale
|
2022-04-12 01:58:52 -04:00
|
|
|
WHERE
|
|
|
|
vscale.factor <> 0
|
|
|
|
)
|
|
|
|
,volume AS (
|
|
|
|
SELECT * FROM volume_only
|
|
|
|
UNION ALL
|
2022-04-12 02:30:26 -04:00
|
|
|
SELECT * FROM basemix WHERE (SELECT factor FROM vscale) = 0
|
2020-12-05 00:10:47 -05:00
|
|
|
)
|
|
|
|
,pscale AS (
|
2022-04-12 02:30:26 -04:00
|
|
|
SELECT
|
|
|
|
(SELECT pincr FROM target) AS target_increment
|
|
|
|
,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
|
|
|
|
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
|
2020-12-05 00:10:47 -05:00
|
|
|
)
|
|
|
|
,pricing AS (
|
|
|
|
SELECT
|
2022-04-09 02:29:51 -04:00
|
|
|
o.id
|
|
|
|
,o.logid
|
|
|
|
,o."Trans. Date"
|
|
|
|
,o."Post Date"
|
|
|
|
,o."Description"
|
2022-04-12 02:30:26 -04:00
|
|
|
,round((CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric,2) AS "Amount"
|
2022-04-09 02:29:51 -04:00
|
|
|
,o."Category"
|
|
|
|
,o."Party"
|
|
|
|
,o."Reason"
|
2022-04-22 00:22:35 -04:00
|
|
|
,0::numeric AS app_units
|
2022-04-12 01:58:52 -04:00
|
|
|
,'app_forecast_name' AS app_version
|
2022-04-12 02:30:26 -04:00
|
|
|
,'scale price' AS app_iter
|
2022-04-12 01:58:52 -04:00
|
|
|
,(SELECT id FROM ilog) AS app_logid
|
2020-12-08 00:49:04 -05:00
|
|
|
FROM
|
|
|
|
volume o
|
|
|
|
CROSS JOIN pscale
|
|
|
|
WHERE
|
|
|
|
pscale.factor <> 0 or pscale.mod_price <> 0
|
2020-12-05 00:10:47 -05:00
|
|
|
)
|
2022-04-22 01:15:51 -04:00
|
|
|
, ins AS (
|
2020-12-05 00:10:47 -05:00
|
|
|
INSERT INTO
|
2022-04-12 01:58:52 -04:00
|
|
|
tpsv.dcard
|
2020-12-08 01:13:31 -05:00
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
2022-04-12 02:30:26 -04:00
|
|
|
volume_only
|
2020-12-08 01:13:31 -05:00
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
pricing
|
2022-04-22 01:15:51 -04:00
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
SELECT COUNT(*) as num_rows FROM ins
|