Compare commits
No commits in common. "a0f78af496d61ede67cf9ce321c7de942ecfed5e" and "f382d256edd43934d05faf10a7b75da7fc099c30" have entirely different histories.
a0f78af496
...
f382d256ed
@ -10,40 +10,14 @@ DECLARE
|
|||||||
_actpy text;
|
_actpy text;
|
||||||
_sql text;
|
_sql text;
|
||||||
_baseline text;
|
_baseline text;
|
||||||
_date_funcs jsonb;
|
|
||||||
_perd_joins text;
|
|
||||||
_interval interval;
|
|
||||||
|
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
-----------------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;
|
|
||||||
--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
|
|
||||||
SELECT
|
|
||||||
string_agg(
|
|
||||||
'LEFT OUTER JOIN fc.perd '||func||' ON'||
|
|
||||||
$$
|
|
||||||
$$||'(o.'||fkey||' + interval '||format('%L',_interval) ||' )::date <@ '||func||'.drange'
|
|
||||||
,E'\n')
|
|
||||||
INTO
|
|
||||||
_perd_joins
|
|
||||||
FROM
|
|
||||||
fc.target_meta
|
|
||||||
WHERE
|
|
||||||
dtype = 'date'
|
|
||||||
AND fkey IS NOT NULL;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS fc.sql(cmd text PRIMARY KEY, t text );
|
CREATE TABLE IF NOT EXISTS fc.sql(cmd text PRIMARY KEY, t text );
|
||||||
|
|
||||||
-------------------------------build a column list-----------------------------------------
|
-------------------------------build a column list----------------------------------------
|
||||||
SELECT
|
SELECT
|
||||||
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
string_agg(format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
||||||
INTO
|
INTO
|
||||||
_clist
|
_clist
|
||||||
FROM
|
FROM
|
||||||
@ -51,60 +25,46 @@ FROM
|
|||||||
WHERE
|
WHERE
|
||||||
func NOT IN ('version');
|
func NOT IN ('version');
|
||||||
|
|
||||||
---------------------------build column to increment dates---------------------------------
|
---------------------------build column to increment dates--------------------------------
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
string_agg(
|
string_agg(
|
||||||
CASE
|
format('%I',cname) || CASE WHEN func IN ('odate','sdate') AND dtype = 'date' THEN ' + interval ''1 year''' ELSE '' END,E'\n ,' ORDER BY opos ASC)
|
||||||
--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
|
|
||||||
'o.'||fkey||' + interval '||format('%L',_interval)
|
|
||||||
END
|
|
||||||
ELSE
|
|
||||||
'o.'||format('%I',cname)
|
|
||||||
END
|
|
||||||
,E'\n ,' ORDER BY opos ASC
|
|
||||||
)
|
|
||||||
INTO
|
INTO
|
||||||
_clist_inc
|
_clist_inc
|
||||||
FROM
|
FROM
|
||||||
fc.target_meta m
|
fc.target_meta
|
||||||
WHERE
|
WHERE
|
||||||
func NOT IN ('version');
|
func NOT IN ('version');
|
||||||
|
|
||||||
--RAISE NOTICE 'build list: %',clist;
|
--RAISE NOTICE 'build list: %',clist;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
--------------------------------------clone the actual baseline-----------------------------------------------
|
--------------------------------------clone the actual baseline-----------------------------------------------
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
$$SELECT
|
$a$SELECT
|
||||||
$$::text||
|
$a$::text||
|
||||||
_clist||
|
_clist||
|
||||||
$$
|
$b$
|
||||||
,'forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'actuals' iter
|
,'actuals' iter
|
||||||
FROM
|
FROM
|
||||||
fc.live o
|
rlarp.osm_dev o
|
||||||
WHERE
|
WHERE
|
||||||
(
|
(
|
||||||
--base period orders booked....
|
--base period orders booked....
|
||||||
$$||_order_date||$$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date]
|
$b$||_order_date||$c$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date]
|
||||||
--...or any open orders currently booked before cutoff....
|
--...or any open orders currently booked before cutoff....
|
||||||
OR ($$||_order_status||$$ IN ([app_openstatus_code]) and $$||_order_date||$$ <= [app_openorder_cutoff])
|
OR ($c$||_order_status||$d$ IN ([app_openstatus_code]) and $d$||_order_date||$e$ <= [app_openorder_cutoff])
|
||||||
--...or anything that shipped in that period
|
--...or anything that shipped in that period
|
||||||
OR ($$||_ship_date||$$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date])
|
OR ($e$||_ship_date||$f$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date])
|
||||||
)
|
)
|
||||||
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
||||||
$$::text
|
$f$::text
|
||||||
INTO
|
INTO
|
||||||
_ytdbody;
|
_ytdbody;
|
||||||
|
|
||||||
@ -120,7 +80,7 @@ $$
|
|||||||
,'forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'plug' iter
|
,'plug' iter
|
||||||
FROM
|
FROM
|
||||||
fc.live o$$||E'\n'||_perd_joins||$$
|
rlarp.osm_dev o
|
||||||
WHERE
|
WHERE
|
||||||
$$||_order_date||$$ BETWEEN [app_plug_fromdate] AND [app_plug_todate]
|
$$||_order_date||$$ BETWEEN [app_plug_fromdate] AND [app_plug_todate]
|
||||||
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
|
||||||
@ -131,19 +91,20 @@ INTO
|
|||||||
------------------------------copy a full year and increment by 1 year for the baseline-------------------------
|
------------------------------copy a full year and increment by 1 year for the baseline-------------------------
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
$$INSERT INTO
|
$a$
|
||||||
|
INSERT INTO
|
||||||
fc.live
|
fc.live
|
||||||
SELECT
|
SELECT
|
||||||
$$||_clist_inc||
|
$a$||_clist_inc||
|
||||||
$$
|
$b$
|
||||||
,'forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'baseline' iter
|
,'baseline' iter
|
||||||
FROM
|
FROM
|
||||||
baseline o$$||E'\n'||_perd_joins||$$
|
baseline
|
||||||
WHERE
|
WHERE
|
||||||
$$||_order_date||' >= [app_first_forecast_date]'||$$
|
$b$||_order_date||$c$ + interval '1 year' >= $c$||'[app_first_order_date_year]'
|
||||||
OR $$||_ship_date||' >= [app_first_forecast_date]'
|
--the final forecast baseline should have orders greater than or equal to the
|
||||||
--any orders in the forecast period, or any sales in the forecast period (from open orders)
|
--start of the year since new orders is the intended forecast
|
||||||
INTO
|
INTO
|
||||||
_baseline;
|
_baseline;
|
||||||
|
|
||||||
@ -158,7 +119,8 @@ $$||_ytdbody||
|
|||||||
$$UNION ALL
|
$$UNION ALL
|
||||||
$$||_actpy
|
$$||_actpy
|
||||||
||$$)
|
||$$)
|
||||||
$$||_baseline
|
$$
|
||||||
|
||_baseline
|
||||||
INTO
|
INTO
|
||||||
_sql;
|
_sql;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user