diff --git a/generate_sql/col_baseline.sql b/generate_sql/col_baseline.sql index 6ba6cde..434e81d 100644 --- a/generate_sql/col_baseline.sql +++ b/generate_sql/col_baseline.sql @@ -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; diff --git a/readme.md b/readme.md index 07f9b15..4814a72 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/setup_sql/schema.sql b/setup_sql/schema.sql index 0a3c03e..f13a9ca 100644 --- a/setup_sql/schema.sql +++ b/setup_sql/schema.sql @@ -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)';