2020-11-28 02:17:53 -05:00
|
|
|
DO
|
|
|
|
$func$
|
|
|
|
DECLARE
|
2022-04-12 01:58:52 -04:00
|
|
|
_clist_all text; --all columns
|
|
|
|
_clist_sum text; --group away forecast iteration rows
|
|
|
|
_clist_group text; --group away forecast iteration rows
|
|
|
|
_clist_vol text; --volume columns scaled
|
|
|
|
_clist_prc text; --price columns scaled
|
|
|
|
_target_table text;
|
|
|
|
_order_date text;
|
|
|
|
_ship_date text;
|
|
|
|
_order_status text;
|
|
|
|
_version_col text;
|
|
|
|
_iter_col text;
|
|
|
|
_logid_col text;
|
|
|
|
_units_col text;
|
|
|
|
_value_col text;
|
|
|
|
_sql text;
|
2020-11-28 02:17:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
2020-12-02 01:51:15 -05:00
|
|
|
-----------------populate application variables--------------------------------------------
|
2022-04-12 01:58:52 -04:00
|
|
|
SELECT cname INTO _order_date FROM fc.target_meta WHERE appcol = 'order_date' ;
|
|
|
|
SELECT cname INTO _ship_date FROM fc.target_meta WHERE appcol = 'ship_date' ;
|
|
|
|
SELECT cname INTO _order_status FROM fc.target_meta WHERE appcol = 'order_status' ;
|
|
|
|
SELECT cname INTO _units_col FROM fc.target_meta WHERE appcol = 'units' ;
|
|
|
|
SELECT cname INTO _value_col FROM fc.target_meta WHERE appcol = 'value' ;
|
|
|
|
SELECT cname INTO _version_col FROM fc.target_meta WHERE appcol = 'version' ;
|
|
|
|
SELECT cname INTO _iter_col FROM fc.target_meta WHERE appcol = 'iter' ;
|
|
|
|
SELECT cname INTO _logid_col FROM fc.target_meta WHERE appcol = 'logid' ;
|
2022-04-09 02:28:53 -04:00
|
|
|
SELECT format('%I',max(schema))||'.'||format('%I',max(tname)) INTO _target_table FROM fc.target_meta;
|
|
|
|
|
|
|
|
RAISE NOTICE '_order_date; %', _order_date;
|
|
|
|
RAISE NOTICE '_ship_date; %', _ship_date;
|
|
|
|
RAISE NOTICE '_order_status;%', _order_status;
|
|
|
|
RAISE NOTICE '_units_col; %', _units_col;
|
|
|
|
RAISE NOTICE '_value_col; %', _value_col;
|
|
|
|
RAISE NOTICE '_target_table;%', _target_table;
|
|
|
|
RAISE NOTICE '_version_col; %', _version_col;
|
2022-04-12 01:58:52 -04:00
|
|
|
RAISE NOTICE '_iter_col; %', _iter_col;
|
|
|
|
RAISE NOTICE '_logid_col; %', _logid_col;
|
2020-12-02 02:05:40 -05:00
|
|
|
-------------------------all columns ------------------------------------------------------
|
2022-04-09 02:28:53 -04:00
|
|
|
|
2020-12-02 01:51:15 -05:00
|
|
|
SELECT
|
|
|
|
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
|
|
|
INTO
|
2022-04-12 01:58:52 -04:00
|
|
|
_clist_all
|
|
|
|
FROM
|
|
|
|
fc.target_meta;
|
|
|
|
|
|
|
|
-------------------------basemix sum columns-----------------------------------------------
|
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
|
|
|
CASE appcol
|
|
|
|
WHEN 'units' THEN 'sum(o.'||format('%I',cname)||') AS '||format('%I',cname)
|
|
|
|
WHEN 'value' THEN 'sum(o.'||format('%I',cname)||') AS '||format('%I',cname)
|
|
|
|
WHEN 'cost' THEN 'sum(o.'||format('%I',cname)||') AS '||format('%I',cname)
|
|
|
|
---if basemix is used in price or volume, these columns are overridden anyways---
|
|
|
|
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
|
|
|
|
WHEN 'iter' THEN '''scale'' AS '||format('%I',_iter_col)
|
|
|
|
WHEN 'logid' THEN '0::bigint AS '||format('%I',_logid_col)
|
|
|
|
ELSE 'o.'||format('%I',cname)
|
|
|
|
END
|
|
|
|
--delimiter
|
|
|
|
,E'\n ,'
|
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_sum
|
|
|
|
FROM
|
|
|
|
fc.target_meta;
|
|
|
|
|
|
|
|
RAISE NOTICE 'sumation columns %', _clist_sum;
|
|
|
|
|
|
|
|
-------------------------basemix group by columns------------------------------------------
|
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
|
|
|
'o.'||format('%I',cname)
|
|
|
|
--delimiter
|
|
|
|
,E'\n ,'
|
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_group
|
2020-12-02 01:51:15 -05:00
|
|
|
FROM
|
|
|
|
fc.target_meta
|
2022-04-12 01:58:52 -04:00
|
|
|
WHERE
|
|
|
|
COALESCe(appcol,'') NOT IN ('version','iter','logid','value','cost','units');
|
2022-04-09 02:28:53 -04:00
|
|
|
|
2022-04-12 01:58:52 -04:00
|
|
|
RAISE NOTICE 'sumation columns %', _clist_group;
|
2022-04-09 02:28:53 -04:00
|
|
|
|
2022-04-12 01:58:52 -04:00
|
|
|
-------------------------all columns as-is except volume types which need scaled-----------------------------------
|
2020-12-02 02:05:40 -05:00
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
2022-04-12 01:58:52 -04:00
|
|
|
CASE appcol
|
|
|
|
----calculation should yield the value necessary to get to the target increment value------------
|
2022-04-12 02:30:26 -04:00
|
|
|
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)
|
2022-04-12 01:58:52 -04:00
|
|
|
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)
|
|
|
|
ELSE 'o.'||format('%I',cname)
|
|
|
|
END
|
2020-12-02 02:05:40 -05:00
|
|
|
--delimiter
|
2022-04-12 01:58:52 -04:00
|
|
|
,E'\n ,'
|
2020-12-02 02:05:40 -05:00
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_vol
|
|
|
|
FROM
|
2022-04-12 01:58:52 -04:00
|
|
|
fc.target_meta;
|
2022-04-09 02:28:53 -04:00
|
|
|
|
|
|
|
RAISE NOTICE ' all columns plus scale volume columns %', _clist_vol;
|
2020-11-28 02:17:53 -05:00
|
|
|
|
2022-04-12 01:58:52 -04:00
|
|
|
-------------------------all columns with price types getting scaled-----------------------------------
|
2020-12-02 02:41:34 -05:00
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
2022-04-12 01:58:52 -04:00
|
|
|
CASE appcol
|
|
|
|
WHEN 'units' THEN '0::numeric AS '||format('%I',cname)
|
|
|
|
WHEN 'cost' THEN '0::numeric AS '||format('%I',cname)
|
2022-04-12 02:30:26 -04:00
|
|
|
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)
|
2022-04-12 01:58:52 -04:00
|
|
|
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
|
2022-04-12 02:30:26 -04:00
|
|
|
WHEN 'iter' THEN '''scale price'' AS '||format('%I',_iter_col)
|
2022-04-12 01:58:52 -04:00
|
|
|
WHEN 'logid' THEN '(SELECT id FROM ilog) AS '||format('%I',_logid_col)
|
2020-12-02 02:41:34 -05:00
|
|
|
ELSE 'o.'||format('%I',cname)
|
2022-04-12 01:58:52 -04:00
|
|
|
END
|
2020-12-02 02:41:34 -05:00
|
|
|
--delimiter
|
2022-04-12 01:58:52 -04:00
|
|
|
,E'\n ,'
|
2020-12-02 02:41:34 -05:00
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_prc
|
|
|
|
FROM
|
2022-04-12 01:58:52 -04:00
|
|
|
fc.target_meta;
|
2022-04-09 02:28:53 -04:00
|
|
|
|
|
|
|
RAISE NOTICE 'all columns plus scale price %', _clist_prc;
|
2020-12-02 02:41:34 -05:00
|
|
|
|
2020-11-28 02:17:53 -05:00
|
|
|
SELECT
|
2020-12-02 01:51:15 -05:00
|
|
|
---------$$app_req$$ will hold the request body--------------------
|
2020-11-28 03:00:23 -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:28:53 -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-02 01:51:15 -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-11-28 03:00:23 -05:00
|
|
|
SELECT
|
2022-04-12 01:58:52 -04:00
|
|
|
sum($$||_units_col||$$) FILTER (WHERE $$||format('%I',_iter_col)||$$ <> 'ACTUALS') total
|
|
|
|
,sum($$||_units_col||$$) FILTER (WHERE $$||format('%I',_iter_col)||$$ = 'baseline') base
|
2020-11-28 03:00:23 -05:00
|
|
|
FROM
|
2022-04-09 02:28:53 -04:00
|
|
|
$$||_target_table||$$ o
|
2020-11-28 03:00:23 -05:00
|
|
|
WHERE
|
|
|
|
app_where
|
2020-11-28 02:17:53 -05:00
|
|
|
)
|
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-02 01:51:15 -05:00
|
|
|
,basemix AS (
|
|
|
|
SELECT
|
2022-04-12 01:58:52 -04:00
|
|
|
$$||_clist_sum||$$
|
2020-12-05 01:19:17 -05:00
|
|
|
FROM
|
2022-04-09 02:28:53 -04:00
|
|
|
$$||_target_table||$$ o
|
2020-12-02 01:51:15 -05:00
|
|
|
WHERE
|
2020-12-05 01:19:17 -05:00
|
|
|
app_where
|
2022-04-12 01:58:52 -04:00
|
|
|
GROUP BY
|
|
|
|
$$||_clist_group||$$
|
2020-12-02 01:51:15 -05:00
|
|
|
),
|
|
|
|
vscale AS (
|
|
|
|
SELECT
|
2020-12-08 00:49:04 -05:00
|
|
|
(SELECT vincr FROM target) AS target_increment
|
2020-12-02 02:05:40 -05:00
|
|
|
,sum($$||_units_col||') AS units'||$$
|
2022-04-09 02:28:53 -04:00
|
|
|
,CASE WHEN sum($$||_units_col||$$) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum($$||_units_col||$$) END AS factor
|
2020-12-05 01:19:17 -05:00
|
|
|
FROM
|
|
|
|
basemix
|
2020-12-02 02:05:40 -05:00
|
|
|
)
|
2022-04-12 01:58:52 -04:00
|
|
|
,volume_only AS (
|
2020-12-02 02:05:40 -05:00
|
|
|
SELECT
|
|
|
|
$$||_clist_vol||$$
|
|
|
|
FROM
|
2020-12-05 01:19:17 -05:00
|
|
|
basemix o
|
2020-12-02 02:05:40 -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-02 02:41:34 -05:00
|
|
|
)
|
|
|
|
,pscale AS (
|
2022-04-12 02:09:36 -04:00
|
|
|
SELECT
|
|
|
|
(SELECT pincr FROM target) AS target_increment
|
|
|
|
,sum($$||format('%I',_value_col)||') AS value'||$$
|
|
|
|
,CASE WHEN (SELECT sum($$||format('%I',_value_col)||$$) FROM volume) = 0 THEN
|
|
|
|
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
|
|
|
|
0
|
|
|
|
ELSE
|
2022-04-12 02:30:26 -04:00
|
|
|
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
|
|
|
|
ELSE ((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
|
|
|
|
END
|
2022-04-12 02:09:36 -04:00
|
|
|
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
|
|
|
|
--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($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_units_col)||$$) FROM volume)
|
|
|
|
ELSE
|
|
|
|
0
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
0
|
|
|
|
END mod_price
|
|
|
|
FROM
|
|
|
|
volume
|
2020-12-02 02:41:34 -05:00
|
|
|
)
|
|
|
|
,pricing AS (
|
|
|
|
SELECT
|
|
|
|
$$||_clist_prc||$$
|
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-02 02:41:34 -05:00
|
|
|
)
|
2022-05-12 00:17:31 -04:00
|
|
|
,ins AS (
|
2020-12-02 02:41:34 -05:00
|
|
|
INSERT INTO
|
2022-04-12 01:58:52 -04:00
|
|
|
$$||_target_table||$$
|
2020-12-08 01:13:31 -05:00
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
2022-04-12 02:09:36 -04:00
|
|
|
volume_only
|
2020-12-08 01:13:31 -05:00
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
pricing
|
2022-05-12 00:17:31 -04:00
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
SELECT count(*) num_rows FROM ins
|
|
|
|
|
2020-12-02 02:41:34 -05:00
|
|
|
$$
|
2020-12-02 01:51:15 -05:00
|
|
|
INTO
|
|
|
|
_sql;
|
2020-11-28 02:17:53 -05:00
|
|
|
|
2020-12-02 01:51:15 -05:00
|
|
|
RAISE NOTICE '%', _sql;
|
|
|
|
|
2022-05-12 22:59:37 -04:00
|
|
|
INSERT INTO fc.sql SELECT _target_table, 'scale', _sql ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED.t;
|
2020-11-28 02:17:53 -05:00
|
|
|
|
|
|
|
END
|
|
|
|
$func$;
|
|
|
|
|
|
|
|
---SELECT * FROM fc.sql;
|