add the notion of an appcolumn, and use it to push into variables to generate route sql

This commit is contained in:
Paul Trowbridge 2020-11-05 23:50:02 -05:00
parent d9bfe43d2e
commit fcac081823
3 changed files with 38 additions and 13 deletions

View File

@ -1,15 +1,21 @@
DO
$$
DECLARE
clist text;
ytdbody text;
_clist text;
_ytdbody text;
_order_date text;
_ship_date text;
_order_status text;
BEGIN
CREATE TABLE IF NOT EXISTS fc.sql(cmd text PRIMARY KEY, t text );
-------------------------------build a column list----------------------------------------
SELECT
string_agg(format('%I',cname),E'\n ,' ORDER BY opos ASC)
INTO
clist
_clist
FROM
fc.target_meta
WHERE
@ -17,11 +23,14 @@ WHERE
--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;
SELECT
$a$
SELECT
$a$SELECT
$a$::text||
clist||
_clist||
$b$
,'baseline' "version"
,'actuals' iter
@ -30,18 +39,22 @@ FROM
WHERE
(
--base period orders booked....
[order date column name] BETWEEN [supplied target range from date] AND [supplied target range 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 ([order status column here] IN ([list of statuses indicating still open]) and [order date column name] <= [include open orders through this date])
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 ([name of shipdate column] BETWEEN [supplied target range from date] AND [supplied target range 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
$b$::text
$f$::text
INTO
ytdbody;
_ytdbody;
RAISE NOTICE '%', ytdbody;
--RAISE NOTICE '%', _ytdbody;
INSERT INTO fc.sql SELECT 'baseline', _ytdbody ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED.t;
END
$$
$$;
SELECT * FROM fc.sql;

View File

@ -35,3 +35,5 @@ to-do:
* problem: how will the incremented order season get updated, adding an interval won't work
* a table fc.odate, has been built, but it is incomplete, a setup function filling in these date-keyed tables could be setup
* if a table is date-keyed, fc.perd could be targeted to fill in the gaps
* the target sales data has to map have concepts like order_date, and the application needs to know which col is order date
* add column called application hook

View File

@ -10,9 +10,19 @@ CREATE TABLE fc.target_meta (
,pretty text
,dtype text
,mastcol text
,appcol text
);
--ALTER TABLE fc.target_meta DROP CONSTRAINT IF EXISTS target_meta_pk;
ALTER TABLE fc.target_meta ADD CONSTRAINT target_meta_pk PRIMARY KEY (tname, cname);
COMMENT ON TABLE fc.target_meta IS 'target table layout info';
COMMENT ON COLUMN fc.target_meta.tname IS 'schema.table_name of target sales data table';
COMMENT ON COLUMN fc.target_meta.cname IS 'column name';
COMMENT ON COLUMN fc.target_meta.opos IS 'ordinal position of column';
COMMENT ON COLUMN fc.target_meta.func IS 'a functional entity (like customer, part number) that master tables will be build from';
COMMENT ON COLUMN fc.target_meta.fkey IS 'primary key for functional entity';
COMMENT ON COLUMN fc.target_meta.pretty IS 'the presentation name of the column';
COMMENT ON COLUMN fc.target_meta.dtype IS 'data type of the sales table column';
COMMENT ON COLUMN fc.target_meta.mastcol IS 'associated field from the master data table if it is different (oseas would refer to ssyr in fc.perd)';
COMMENT ON COLUMN fc.target_meta.appcol IS 'supply column name to be used for application variables - (specifcy the order date column)';