pivot_forecast/routes/scale/gen_scale.sql

210 lines
5.9 KiB
MySQL
Raw Normal View History

2020-11-28 02:17:53 -05:00
DO
$func$
DECLARE
_clist text;
2020-12-02 02:05:40 -05:00
_clist_vol text;
2020-12-02 02:41:34 -05:00
_clist_prc text;
2020-11-28 02:17:53 -05:00
_clist_inc text;
_ytdbody text;
_order_date text;
_ship_date text;
_order_status text;
_actpy text;
_sql text;
2022-04-09 02:28:53 -04:00
_target_table text;
_version_col text;
2020-11-28 02:17:53 -05:00
_baseline text;
_date_funcs jsonb;
_perd_joins text;
_interval interval;
_units_col text;
_value_col text;
2020-11-28 02:17:53 -05:00
BEGIN
-----------------populate application variables--------------------------------------------
--need a handler if any of these are not specified--
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_date') INTO _order_date;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'ship_date') INTO _ship_date;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_status') INTO _order_status;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'units') INTO _units_col;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'value') INTO _value_col;
2022-04-09 02:28:53 -04:00
SELECT format('%I',max(schema))||'.'||format('%I',max(tname)) INTO _target_table FROM fc.target_meta;
SELECT cname INTO _version_col FROM fc.target_meta WHERE appcol = 'version';
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;
2020-12-02 02:05:40 -05:00
-------------------------all columns ------------------------------------------------------
2022-04-09 02:28:53 -04:00
SELECT
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
INTO
_clist
FROM
fc.target_meta
WHERE
2022-04-09 02:28:53 -04:00
COALESCE(appcol,'') NOT IN ('version','iter','logid');
RAISE NOTICE 'all columns %', _clist;
2020-12-02 02:41:34 -05:00
-------------------------all columns except volume scale-----------------------------------
2020-12-02 02:05:40 -05:00
SELECT
string_agg(
--create the column reference
'o.'||format('%I',cname)||
2020-12-05 01:19:17 -05:00
CASE WHEN appcol IN ('units', 'value', 'cost') THEN ' * vscale.factor AS '||format('%I',cname) ELSE '' END,
2020-12-02 02:05:40 -05:00
--delimiter
E'\n ,'
--sort column ordinal
ORDER BY opos ASC
)
INTO
_clist_vol
FROM
fc.target_meta
WHERE
2022-04-09 02:28:53 -04:00
COALESCE(appcol,'') NOT IN ('version','iter','logid');
RAISE NOTICE ' all columns plus scale volume columns %', _clist_vol;
2020-11-28 02:17:53 -05:00
2020-12-02 02:41:34 -05:00
-------------------------all columns except volume scale-----------------------------------
SELECT
string_agg(
--create the column reference
CASE
WHEN appcol IN ('units', 'cost') THEN '0::numeric'
WHEN appcol IN ('value') THEN $$(CASE WHEN pscale.factor = 0 THEN o.$$||_units_col||$$ * pscale.mod_price ELSE o.$$||format('%I',cname)||' * pscale.factor END)::numeric AS '||_value_col
ELSE 'o.'||format('%I',cname)
END,
--delimiter
E'\n ,'
--sort column ordinal
ORDER BY opos ASC
)
INTO
_clist_prc
FROM
fc.target_meta
WHERE
2022-04-09 02:28:53 -04:00
COALESCE(appcol,'') NOT IN ('version','iter','logid');
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
---------$$app_req$$ will hold the request body--------------------
$$WITH
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
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
2020-12-05 01:19:17 -05:00
sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total
,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base
FROM
2022-04-09 02:28:53 -04:00
$$||_target_table||$$ o
WHERE
app_where
2020-11-28 02:17:53 -05:00
)
,basemix AS (
SELECT
$$||_clist||$$
2020-12-05 01:19:17 -05:00
FROM
2022-04-09 02:28:53 -04:00
$$||_target_table||$$ o
WHERE
2020-12-05 01:19:17 -05:00
app_where
),
vscale AS (
SELECT
(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
)
2020-12-08 01:30:02 -05:00
,ilog AS (
INSERT INTO
fc.log (doc)
SELECT
req.j
FROM
req
)
2020-12-02 02:05:40 -05:00
,volume AS (
SELECT
$$||_clist_vol||$$
2022-04-09 02:28:53 -04:00
,'app_forecast_name' AS version
2020-12-08 01:13:31 -05:00
,'scale vol' AS iter
2020-12-02 02:05:40 -05:00
FROM
2020-12-05 01:19:17 -05:00
basemix o
2020-12-02 02:05:40 -05:00
CROSS JOIN vscale
2020-12-02 02:41:34 -05:00
)
,pscale AS (
SELECT
(SELECT pincr FROM target) AS target_increment
2022-04-09 02:28:53 -04:00
,sum($$||format('%I',_value_col)||') AS value'||$$
,CASE WHEN (SELECT sum($$||format('%I',_value_col)||$$) FROM volume) = 0 THEN
2020-12-02 02:41:34 -05:00
--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:28:53 -04:00
((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
2020-12-02 02:41:34 -05:00
END factor
2022-04-09 02:28:53 -04:00
,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
2020-12-02 02:41:34 -05:00
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply
2022-04-09 02:28:53 -04:00
((SELECT pincr::numeric FROM target) - (SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_units_col)||$$) FROM volume)
2020-12-02 02:41:34 -05:00
ELSE
0
END
ELSE
0
END mod_price
2020-12-05 01:19:17 -05:00
FROM
volume
2020-12-02 02:41:34 -05:00
)
,pricing AS (
SELECT
$$||_clist_prc||$$
2022-04-09 02:28:53 -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
2020-12-02 02:41:34 -05:00
)
INSERT INTO
2022-04-09 02:28:53 -04:00
$$||_target_table||$$ o
2020-12-08 01:13:31 -05:00
SELECT
*
FROM
volume
UNION ALL
SELECT
*
FROM
pricing
2020-12-02 02:41:34 -05:00
$$
INTO
_sql;
2020-11-28 02:17:53 -05:00
RAISE NOTICE '%', _sql;
INSERT INTO fc.sql SELECT 'scale', _sql ON CONFLICT (cmd) DO UPDATE SET t = EXCLUDED.t;
2020-11-28 02:17:53 -05:00
END
$func$;
---SELECT * FROM fc.sql;