2020-10-30 00:56:48 -04:00
|
|
|
DO
|
2020-11-05 23:58:29 -05:00
|
|
|
$func$
|
2020-10-30 00:56:48 -04:00
|
|
|
DECLARE
|
2022-04-13 01:17:04 -04:00
|
|
|
_clist text;
|
|
|
|
_clist_sum text;
|
|
|
|
_clist_group text;
|
|
|
|
_clist_inc text;
|
|
|
|
_version_col text;
|
|
|
|
_iter_col text;
|
|
|
|
_logid_col text;
|
|
|
|
_order_date text;
|
|
|
|
_ship_date text;
|
|
|
|
_order_status text;
|
|
|
|
_units_col text;
|
|
|
|
_value_col text;
|
|
|
|
_ytdbody text;
|
|
|
|
_actpy text;
|
|
|
|
_sql text;
|
|
|
|
_baseline text;
|
|
|
|
_date_funcs jsonb;
|
|
|
|
_perd_joins text;
|
|
|
|
_interval interval;
|
|
|
|
_target_table text;
|
2020-10-30 00:56:48 -04:00
|
|
|
|
2020-11-12 00:22:00 -05:00
|
|
|
/*----------------parameters listing--------------
|
|
|
|
app_baseline_from_date
|
|
|
|
app_baseline_to_date
|
|
|
|
app_first_forecast_date
|
|
|
|
app_openorder_cutoff
|
|
|
|
app_openstatus_code
|
|
|
|
app_plug_fromdate
|
|
|
|
app_plug_todate
|
|
|
|
------------------------------------------------*/
|
|
|
|
|
2020-10-30 00:56:48 -04:00
|
|
|
BEGIN
|
2020-11-05 23:50:02 -05:00
|
|
|
|
2020-11-09 23:45:18 -05:00
|
|
|
-----------------populate application variables--------------------------------------------
|
2022-04-13 01:17:04 -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' ;
|
|
|
|
SELECT format('%I',max(schema))||'.'||format('%I',max(tname)) INTO _target_table FROM fc.target_meta;
|
2020-11-10 00:29:57 -05:00
|
|
|
SELECT interval '1 year' INTO _interval;
|
2022-04-13 01:17:04 -04:00
|
|
|
|
|
|
|
----------------------setup listing of date joins required-----------------------------------------------------
|
|
|
|
SELECT jsonb_agg(DISTINCT func) INTO _date_funcs FROM fc.target_meta WHERE dtype = 'date' AND fkey is NOT null;
|
|
|
|
raise notice E'date list -----> \n %',_date_funcs;
|
|
|
|
|
|
|
|
-----------------------create the date based table joins--------------------------------------------------------
|
2020-11-10 00:29:57 -05:00
|
|
|
--create table join for each date based func in target_meta joining to fc.perd static table
|
|
|
|
--the join, though, should be based on the target date, which is needs an interval added to get to the target
|
2020-11-09 23:45:18 -05:00
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
'LEFT OUTER JOIN fc.perd '||func||' ON'||
|
|
|
|
$$
|
2022-04-05 20:58:22 -04:00
|
|
|
$$||'(o.'||format('%I',cname)||' + interval '||format('%L',_interval) ||' )::date <@ '||func||'.drange'
|
2022-04-13 01:17:04 -04:00
|
|
|
,E'\n ')
|
2020-11-09 23:45:18 -05:00
|
|
|
INTO
|
|
|
|
_perd_joins
|
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
|
|
|
dtype = 'date'
|
2022-04-06 02:32:20 -04:00
|
|
|
AND func IS NOT NULL;
|
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
raise notice E'period table joins -----> \n %',_perd_joins;
|
2020-11-09 23:45:18 -05:00
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
-------------------------------build a column for sums-----------------------------------------
|
2020-10-28 00:03:13 -04:00
|
|
|
SELECT
|
2022-04-13 01:17:04 -04:00
|
|
|
--string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
|
|
|
CASE appcol
|
|
|
|
----aggregation columns-----------------------------------------------------------
|
|
|
|
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)
|
|
|
|
----create singular values for fingle forecast step-------------------------------
|
|
|
|
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
|
|
|
|
WHEN 'iter' THEN '''baseline'' AS '||format('%I',_iter_col)
|
|
|
|
WHEN 'logid' THEN '(SELECT id FROM ilog) AS '||format('%I',_logid_col)
|
|
|
|
ELSE
|
|
|
|
---if there is no function, make the column null or the default value---
|
|
|
|
CASE WHEN t.func IS NULL
|
|
|
|
THEN 'null::'||t.dtype||' AS '||format('%I',cname)
|
|
|
|
ELSE 'o.'||format('%I',cname)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
--delimiter
|
|
|
|
,E'\n ,'
|
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
2020-10-30 00:56:48 -04:00
|
|
|
INTO
|
2022-04-13 01:17:04 -04:00
|
|
|
_clist_sum
|
2020-10-28 00:03:13 -04:00
|
|
|
FROM
|
2022-04-13 01:17:04 -04:00
|
|
|
fc.target_meta t;
|
2020-10-30 00:56:48 -04:00
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
raise notice E'column list for sums -----> \n %',_clist_sum;
|
|
|
|
-------------------------------build a column for groupby-----------------------------------------
|
|
|
|
SELECT
|
|
|
|
--string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
|
|
|
string_agg(
|
|
|
|
--create the column reference
|
|
|
|
'o.'||format('%I',cname)
|
|
|
|
--delimiter
|
|
|
|
,E'\n ,'
|
|
|
|
--sort column ordinal
|
|
|
|
ORDER BY opos ASC
|
|
|
|
)
|
|
|
|
INTO
|
|
|
|
_clist_group
|
|
|
|
FROM
|
|
|
|
fc.target_meta t
|
|
|
|
WHERE
|
|
|
|
COALESCE(appcol,'') NOT IN ('units','value','cost','version','iter','logid')
|
|
|
|
AND func IS NOT NULL;
|
|
|
|
|
|
|
|
raise notice E'column list for group by-----> \n %',_clist_group;
|
2020-11-09 23:45:18 -05:00
|
|
|
---------------------------build column to increment dates---------------------------------
|
2020-11-06 00:12:05 -05:00
|
|
|
|
|
|
|
SELECT
|
|
|
|
string_agg(
|
2020-11-10 01:12:17 -05:00
|
|
|
CASE
|
|
|
|
--if you're dealing with a date function...
|
|
|
|
WHEN _date_funcs ? func THEN
|
|
|
|
CASE
|
|
|
|
--...but it's not the date itself...
|
2022-04-13 01:17:04 -04:00
|
|
|
WHEN fkey IS NULL THEN
|
2020-11-10 01:12:17 -05:00
|
|
|
--...pull the associated date field from perd table
|
2022-04-06 02:32:20 -04:00
|
|
|
'perd.'||m.fcol
|
2020-11-10 01:12:17 -05:00
|
|
|
--...and it's the primary key date...
|
|
|
|
ELSE
|
|
|
|
--use the date key but increment by the target interval
|
|
|
|
--this assumes that the primary key for the func is a date, but it has to be or it wont join anyways
|
2022-04-05 20:58:22 -04:00
|
|
|
'o.'||format('%I',cname)||' + interval '||format('%L',_interval) ||' AS '||format('%I',cname)
|
2020-11-10 01:12:17 -05:00
|
|
|
END
|
|
|
|
ELSE
|
2022-04-13 01:17:04 -04:00
|
|
|
CASE appcol
|
|
|
|
----aggregation columns-----------------------------------------------------------
|
|
|
|
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)
|
|
|
|
----create singular values for fingle forecast step-------------------------------
|
|
|
|
WHEN 'version' THEN '''app_forecast_name'' AS '||format('%I',_version_col)
|
|
|
|
WHEN 'iter' THEN '''baseline'' AS '||format('%I',_iter_col)
|
|
|
|
WHEN 'logid' THEN '(SELECT id FROM ilog) AS '||format('%I',_logid_col)
|
|
|
|
ELSE
|
|
|
|
---if there is no function, make the column null or the default value---
|
|
|
|
CASE WHEN m.func IS NULL
|
|
|
|
THEN 'null::'||m.dtype||' AS '||format('%I',cname)
|
|
|
|
ELSE 'o.'||format('%I',cname)
|
|
|
|
END
|
|
|
|
END
|
2020-11-10 00:29:57 -05:00
|
|
|
END
|
|
|
|
,E'\n ,' ORDER BY opos ASC
|
|
|
|
)
|
2020-11-06 00:12:05 -05:00
|
|
|
INTO
|
|
|
|
_clist_inc
|
|
|
|
FROM
|
2022-04-13 01:17:04 -04:00
|
|
|
fc.target_meta m;
|
|
|
|
--WHERE
|
|
|
|
-- COALESCE(appcol,'') NOT IN ('version','iter','logid');
|
2020-11-06 00:12:05 -05:00
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
RAISE NOTICE E'DATES INCREMENTED: ------> \n %',_clist_inc;
|
2020-11-05 23:01:03 -05:00
|
|
|
|
2020-11-06 00:00:34 -05:00
|
|
|
--------------------------------------clone the actual baseline-----------------------------------------------
|
|
|
|
|
2020-11-05 23:01:03 -05:00
|
|
|
SELECT
|
2020-11-10 00:29:57 -05:00
|
|
|
$$SELECT
|
2022-04-13 01:17:04 -04:00
|
|
|
$$||
|
|
|
|
_clist_sum||
|
2020-11-10 00:29:57 -05:00
|
|
|
$$
|
2020-11-05 23:01:03 -05:00
|
|
|
FROM
|
2022-04-06 01:52:18 -04:00
|
|
|
$$||_target_table||$$ o
|
2020-11-05 23:01:03 -05:00
|
|
|
WHERE
|
|
|
|
(
|
|
|
|
--base period orders booked....
|
2022-04-03 00:49:54 -04:00
|
|
|
$$||format('%I',_order_date)||$$ BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date
|
2020-11-05 23:01:03 -05:00
|
|
|
--...or any open orders currently booked before cutoff....
|
2022-04-03 01:18:14 -04:00
|
|
|
OR ($$||format('%I',_order_status)||$$ IN (app_openstatus_code) and $$||format('%I',_order_date)||$$ <= 'app_openorder_cutoff'::date)
|
2020-11-05 23:01:03 -05:00
|
|
|
--...or anything that shipped in that period
|
2022-04-03 00:49:54 -04:00
|
|
|
OR ($$||format('%I',_ship_date)||$$ BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date)
|
2020-11-05 23:01:03 -05:00
|
|
|
)
|
|
|
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
2022-04-13 01:17:04 -04:00
|
|
|
GROUP BY
|
|
|
|
$$||
|
|
|
|
_clist_group
|
2020-11-05 23:01:03 -05:00
|
|
|
INTO
|
2020-11-05 23:50:02 -05:00
|
|
|
_ytdbody;
|
2020-11-05 23:01:03 -05:00
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
RAISE NOTICE E'_ytdbody \n%', _ytdbody;
|
2020-11-06 00:00:34 -05:00
|
|
|
|
|
|
|
------------------------------------pull a plug from actuals to create a full year baseline------------------
|
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
SELECT
|
2020-11-06 00:12:05 -05:00
|
|
|
$$SELECT
|
|
|
|
$$||_clist_inc||
|
2020-11-05 23:58:29 -05:00
|
|
|
$$
|
|
|
|
FROM
|
2022-04-13 01:17:04 -04:00
|
|
|
$$||_target_table||' o'||E'\n '||_perd_joins||$$
|
2020-11-05 23:58:29 -05:00
|
|
|
WHERE
|
2022-04-03 01:18:14 -04:00
|
|
|
$$||format('%I',_order_date)||$$ BETWEEN 'app_plug_fromdate'::date AND 'app_plug_todate'::date
|
2020-11-05 23:58:29 -05:00
|
|
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
2022-04-13 01:17:04 -04:00
|
|
|
GROUP BY
|
|
|
|
$$||_clist_group
|
2020-11-05 23:58:29 -05:00
|
|
|
INTO
|
|
|
|
_actpy;
|
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
RAISE NOTICE E'plug body --------> \n%',_actpy;
|
2020-11-08 21:04:50 -05:00
|
|
|
------------------------------copy a full year and increment by 1 year for the baseline-------------------------
|
|
|
|
|
|
|
|
SELECT
|
2020-11-24 01:22:46 -05:00
|
|
|
--$$INSERT INTO
|
|
|
|
-- fc.live
|
|
|
|
$$,incr AS (
|
2020-11-08 22:16:40 -05:00
|
|
|
SELECT
|
2022-04-13 01:34:54 -04:00
|
|
|
$$||_clist_inc||$$
|
2020-11-08 21:04:50 -05:00
|
|
|
FROM
|
2020-11-10 00:29:57 -05:00
|
|
|
baseline o$$||E'\n'||_perd_joins||$$
|
2022-04-13 01:34:54 -04:00
|
|
|
GROUP BY
|
|
|
|
$$||_clist_group||$$
|
2020-11-24 01:22:46 -05:00
|
|
|
)
|
2020-11-27 02:24:20 -05:00
|
|
|
,ins AS (
|
2020-11-24 01:22:46 -05:00
|
|
|
INSERT INTO
|
2022-04-06 01:52:18 -04:00
|
|
|
$$||_target_table||$$
|
2020-11-24 01:22:46 -05:00
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
incr i
|
2020-11-08 22:16:40 -05:00
|
|
|
WHERE
|
2022-04-03 01:18:14 -04:00
|
|
|
i.$$||format('%I',_order_date)||$$ >= 'app_first_forecast_date'::date$$||$$
|
|
|
|
OR i.$$||format('%I',_ship_date)||$$ >= 'app_first_forecast_date'::date$$||$$
|
2020-11-27 02:24:20 -05:00
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
SELECT COUNT(*) num_rows FROM ins$$
|
2020-11-10 01:12:17 -05:00
|
|
|
--any orders in the forecast period, or any sales in the forecast period (from open orders)
|
2020-11-08 21:04:50 -05:00
|
|
|
INTO
|
|
|
|
_baseline;
|
|
|
|
|
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
RAISE NOTICE E'_baseline -------> \n%',_baseline;
|
2020-11-06 00:00:34 -05:00
|
|
|
------------------------------stack the sql into the final format------------------------------------------------
|
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
SELECT
|
2022-04-09 02:26:38 -04:00
|
|
|
$$DELETE FROM $$||_target_table||$$ WHERE $$||_version_col||$$ = 'app_forecast_name';
|
2020-11-08 22:16:40 -05:00
|
|
|
WITH
|
2022-04-13 01:34:54 -04:00
|
|
|
req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
|
|
|
|
,ilog AS (
|
2022-04-13 01:17:04 -04:00
|
|
|
INSERT INTO
|
|
|
|
fc.log (doc)
|
|
|
|
SELECT
|
|
|
|
req.j
|
|
|
|
FROM
|
|
|
|
req
|
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
,baseline AS (
|
|
|
|
$$||_ytdbody||$$
|
|
|
|
UNION ALL
|
2020-11-06 00:12:05 -05:00
|
|
|
$$||_actpy
|
2020-11-08 21:04:50 -05:00
|
|
|
||$$)
|
2020-11-10 00:29:57 -05:00
|
|
|
$$||_baseline
|
2020-11-05 23:58:29 -05:00
|
|
|
INTO
|
|
|
|
_sql;
|
2020-11-05 23:50:02 -05:00
|
|
|
|
2022-04-13 01:17:04 -04:00
|
|
|
RAISE NOTICE E'_sql ---------> \n%',_sql;
|
2022-04-06 02:32:20 -04:00
|
|
|
|
2022-05-12 22:59:37 -04:00
|
|
|
INSERT INTO fc.sql SELECT _target_table, 'baseline', _sql ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED.t;
|
2020-10-30 00:56:48 -04:00
|
|
|
|
|
|
|
END
|
2020-11-05 23:58:29 -05:00
|
|
|
$func$;
|
2020-11-05 23:50:02 -05:00
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
---SELECT * FROM fc.sql;
|