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-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;
|
|
|
|
_baseline text;
|
|
|
|
_date_funcs jsonb;
|
|
|
|
_perd_joins text;
|
|
|
|
_interval interval;
|
2020-12-02 01:51:15 -05:00
|
|
|
_units_col text;
|
|
|
|
_value_col text;
|
2020-11-28 02:17:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
2020-12-02 01:51:15 -05:00
|
|
|
-----------------populate application variables--------------------------------------------
|
|
|
|
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;
|
2020-12-02 02:05:40 -05:00
|
|
|
-------------------------all columns ------------------------------------------------------
|
2020-12-02 01:51:15 -05:00
|
|
|
SELECT
|
|
|
|
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
|
|
|
INTO
|
|
|
|
_clist
|
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
|
|
|
func NOT IN ('version');
|
2020-12-02 02:05:40 -05:00
|
|
|
-------------------------all columns except scale-------------------------------------------
|
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
|
|
|
'o.'||format('%I',cname)||
|
|
|
|
CASE WHEN appcol IN ('units', 'value', 'cost') THEN ' * vscale.factor' ELSE '' END,
|
|
|
|
--delimiter
|
|
|
|
E'\n ,'
|
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_vol
|
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
|
|
|
func NOT IN ('version');
|
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
|
|
|
|
req AS (SELECT $$||'$$app_req$$::jsonb)'||$$
|
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-11-28 03:00:23 -05:00
|
|
|
test AS (
|
|
|
|
SELECT
|
|
|
|
sum(app_units) FILTER WHERE (version <> 'ACTUALS') total
|
|
|
|
,sum(app_units) FILTER (WHERE iter = 'baseline') base
|
|
|
|
FROM
|
|
|
|
fc.live
|
|
|
|
WHERE
|
|
|
|
app_where
|
2020-11-28 02:17:53 -05:00
|
|
|
)
|
2020-12-02 01:51:15 -05:00
|
|
|
,basemix AS (
|
|
|
|
SELECT
|
|
|
|
$$||_clist||$$
|
|
|
|
WHERE
|
|
|
|
app_scenario
|
|
|
|
),
|
|
|
|
vscale AS (
|
|
|
|
SELECT
|
|
|
|
app_vincr AS target_increment
|
2020-12-02 02:05:40 -05:00
|
|
|
,sum($$||_units_col||') AS units'||$$
|
2020-12-02 01:51:15 -05:00
|
|
|
,app_vincr/sum($$||_units_col||$$) factor
|
2020-12-02 02:05:40 -05:00
|
|
|
)
|
|
|
|
,volume AS (
|
|
|
|
SELECT
|
|
|
|
$$||_clist_vol||$$
|
|
|
|
FROM
|
|
|
|
baseline
|
|
|
|
CROSS JOIN vscale
|
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;
|
|
|
|
|
2020-11-28 03:00:23 -05:00
|
|
|
INSERT INTO fc.sql SELECT '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;
|