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
|
2020-11-05 23:50:02 -05:00
|
|
|
_clist text;
|
2020-11-06 00:12:05 -05:00
|
|
|
_clist_inc text;
|
2020-11-05 23:50:02 -05:00
|
|
|
_ytdbody text;
|
|
|
|
_order_date text;
|
|
|
|
_ship_date text;
|
|
|
|
_order_status text;
|
2020-11-05 23:58:29 -05:00
|
|
|
_actpy text;
|
|
|
|
_sql text;
|
2020-11-08 21:04:50 -05:00
|
|
|
_baseline text;
|
2020-11-10 00:29:57 -05:00
|
|
|
_date_funcs jsonb;
|
2020-11-09 23:45:18 -05:00
|
|
|
_perd_joins text;
|
2020-11-10 00:29:57 -05:00
|
|
|
_interval interval;
|
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--------------------------------------------
|
|
|
|
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;
|
2020-11-10 00:29:57 -05:00
|
|
|
--the target interval
|
|
|
|
SELECT interval '1 year' INTO _interval;
|
|
|
|
SELECT jsonb_agg(func) INTO _date_funcs FROM fc.target_meta WHERE dtype = 'date' AND fkey is NOT null;
|
|
|
|
--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'||
|
|
|
|
$$
|
2020-11-10 01:12:17 -05:00
|
|
|
$$||'(o.'||fkey||' + interval '||format('%L',_interval) ||' )::date <@ '||func||'.drange'
|
2020-11-09 23:45:18 -05:00
|
|
|
,E'\n')
|
|
|
|
INTO
|
|
|
|
_perd_joins
|
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
|
|
|
dtype = 'date'
|
|
|
|
AND fkey IS NOT NULL;
|
|
|
|
|
|
|
|
-------------------------------build a column list-----------------------------------------
|
2020-10-28 00:03:13 -04:00
|
|
|
SELECT
|
2020-11-10 01:12:17 -05:00
|
|
|
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
2020-10-30 00:56:48 -04:00
|
|
|
INTO
|
2020-11-05 23:50:02 -05:00
|
|
|
_clist
|
2020-10-28 00:03:13 -04:00
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
2020-10-30 00:56:48 -04:00
|
|
|
func NOT IN ('version');
|
|
|
|
|
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...
|
|
|
|
WHEN fkey IS NULL THEN
|
|
|
|
--...pull the associated date field from perd table
|
|
|
|
func||'.'||m.dateref
|
|
|
|
--...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
|
2020-11-24 01:22:46 -05:00
|
|
|
'o.'||fkey||' + interval '||format('%L',_interval) ||' AS '||fkey
|
2020-11-10 01:12:17 -05:00
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
'o.'||format('%I',cname)
|
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
|
2020-11-10 01:12:17 -05:00
|
|
|
fc.target_meta m
|
2020-11-06 00:12:05 -05:00
|
|
|
WHERE
|
|
|
|
func NOT IN ('version');
|
|
|
|
|
2020-11-05 23:01:03 -05:00
|
|
|
--RAISE NOTICE 'build list: %',clist;
|
|
|
|
|
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
|
|
|
|
$$::text||
|
2020-11-05 23:50:02 -05:00
|
|
|
_clist||
|
2020-11-10 00:29:57 -05:00
|
|
|
$$
|
2020-11-08 22:16:40 -05:00
|
|
|
,'forecast_name' "version"
|
2020-11-05 23:01:03 -05:00
|
|
|
,'actuals' iter
|
|
|
|
FROM
|
2020-11-10 00:29:57 -05:00
|
|
|
fc.live 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 00:49:54 -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
|
2020-11-10 00:29:57 -05:00
|
|
|
$$::text
|
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
|
|
|
|
2020-11-05 23:50:02 -05:00
|
|
|
--RAISE NOTICE '%', _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
|
|
|
$$
|
2020-11-08 22:16:40 -05:00
|
|
|
,'forecast_name' "version"
|
2020-11-05 23:58:29 -05:00
|
|
|
,'plug' iter
|
|
|
|
FROM
|
2020-11-10 00:29:57 -05:00
|
|
|
fc.live o$$||E'\n'||_perd_joins||$$
|
2020-11-05 23:58:29 -05:00
|
|
|
WHERE
|
2020-11-24 01:22:46 -05:00
|
|
|
$$||_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
|
|
|
|
$$
|
|
|
|
INTO
|
|
|
|
_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
|
2020-11-10 00:29:57 -05:00
|
|
|
$$||_clist_inc||
|
|
|
|
$$
|
2020-11-09 23:03:57 -05:00
|
|
|
,'forecast_name' "version"
|
|
|
|
,'baseline' iter
|
2020-11-08 21:04:50 -05:00
|
|
|
FROM
|
2020-11-10 00:29:57 -05:00
|
|
|
baseline o$$||E'\n'||_perd_joins||$$
|
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
|
|
|
|
fc.live
|
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
incr i
|
2020-11-08 22:16:40 -05:00
|
|
|
WHERE
|
2020-11-24 01:22:46 -05:00
|
|
|
i.$$||_order_date||$$ >= 'app_first_forecast_date'::date$$||$$
|
2020-11-27 02:24:20 -05:00
|
|
|
OR i.$$||_ship_date||$$ >= 'app_first_forecast_date'::date$$||$$
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2020-11-06 00:00:34 -05:00
|
|
|
------------------------------stack the sql into the final format------------------------------------------------
|
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
SELECT
|
2020-11-08 22:16:40 -05:00
|
|
|
$$DELETE FROM fc.live WHERE version = 'forecast_name';
|
|
|
|
WITH
|
2020-11-08 21:04:50 -05:00
|
|
|
baseline AS (
|
|
|
|
$$||_ytdbody||
|
2020-11-06 00:12:05 -05:00
|
|
|
$$UNION ALL
|
|
|
|
$$||_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
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
INSERT INTO fc.sql SELECT '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;
|