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-10-30 00:56:48 -04:00
|
|
|
|
|
|
|
BEGIN
|
2020-11-05 23:50:02 -05:00
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS fc.sql(cmd text PRIMARY KEY, t text );
|
|
|
|
|
2020-10-30 00:56:48 -04:00
|
|
|
-------------------------------build a column list----------------------------------------
|
2020-10-28 00:03:13 -04:00
|
|
|
SELECT
|
2020-11-05 23:01:03 -05:00
|
|
|
string_agg(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-06 00:12:05 -05:00
|
|
|
---------------------------build column to increment dates--------------------------------
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
string_agg(
|
|
|
|
format('%I',cname) || CASE WHEN func IN ('odate','sdate') AND dtype = 'date' THEN ' + interval ''1 year''' ELSE '' END,E'\n ,' ORDER BY opos ASC)
|
|
|
|
INTO
|
|
|
|
_clist_inc
|
|
|
|
FROM
|
|
|
|
fc.target_meta
|
|
|
|
WHERE
|
|
|
|
func NOT IN ('version');
|
|
|
|
|
2020-11-05 23:01:03 -05:00
|
|
|
--RAISE NOTICE 'build list: %',clist;
|
|
|
|
|
2020-11-05 23:50:02 -05:00
|
|
|
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-06 00:00:34 -05:00
|
|
|
--------------------------------------clone the actual baseline-----------------------------------------------
|
|
|
|
|
2020-11-05 23:01:03 -05:00
|
|
|
SELECT
|
2020-11-05 23:50:02 -05:00
|
|
|
$a$SELECT
|
2020-11-05 23:01:03 -05:00
|
|
|
$a$::text||
|
2020-11-05 23:50:02 -05:00
|
|
|
_clist||
|
2020-11-05 23:01:03 -05:00
|
|
|
$b$
|
|
|
|
,'baseline' "version"
|
|
|
|
,'actuals' iter
|
|
|
|
FROM
|
|
|
|
rlarp.osm_dev o
|
|
|
|
WHERE
|
|
|
|
(
|
|
|
|
--base period orders booked....
|
2020-11-05 23:50:02 -05:00
|
|
|
$b$||_order_date||$c$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date]
|
2020-11-05 23:01:03 -05:00
|
|
|
--...or any open orders currently booked before cutoff....
|
2020-11-05 23:50:02 -05:00
|
|
|
OR ($c$||_order_status||$d$ IN ([app_openstatus_code]) and $d$||_order_date||$e$ <= [app_openorder_cutoff])
|
2020-11-05 23:01:03 -05:00
|
|
|
--...or anything that shipped in that period
|
2020-11-05 23:50:02 -05:00
|
|
|
OR ($e$||_ship_date||$f$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_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-05 23:50:02 -05:00
|
|
|
$f$::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
|
|
|
$$
|
|
|
|
,'baseline' "version"
|
|
|
|
,'plug' iter
|
|
|
|
FROM
|
|
|
|
rlarp.osm_dev o
|
|
|
|
WHERE
|
2020-11-06 00:12:05 -05:00
|
|
|
$$||_order_date||$$ BETWEEN [app_plug_fromdate] AND [app_plug_todate]
|
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-06 00:00:34 -05:00
|
|
|
------------------------------stack the sql into the final format------------------------------------------------
|
|
|
|
|
2020-11-05 23:58:29 -05:00
|
|
|
SELECT
|
2020-11-06 00:12:05 -05:00
|
|
|
_ytdbody||
|
|
|
|
$$UNION ALL
|
|
|
|
$$||_actpy
|
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;
|