From 39a8454b0e750123047865ac5b535c2dfbb2d176 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 24 Nov 2020 01:22:46 -0500 Subject: [PATCH] name the column after incrementing, put variables in single quotes, move the final filter to another layer --- generate_sql/gen_baseline.sql | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/generate_sql/gen_baseline.sql b/generate_sql/gen_baseline.sql index 5e4c162..7eb03aa 100644 --- a/generate_sql/gen_baseline.sql +++ b/generate_sql/gen_baseline.sql @@ -77,7 +77,7 @@ SELECT 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) + 'o.'||fkey||' + interval '||format('%L',_interval) ||' AS '||fkey END ELSE 'o.'||format('%I',cname) @@ -107,11 +107,11 @@ FROM WHERE ( --base period orders booked.... - $$||_order_date||$$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date] + $$||_order_date||$$ BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date --...or any open orders currently booked before cutoff.... - OR ($$||_order_status||$$ IN ([app_openstatus_code]) and $$||_order_date||$$ <= [app_openorder_cutoff]) + OR ($$||_order_status||$$ IN (app_openstatus_code) and $$||_order_date||$$ <= 'app_openorder_cutoff'::date) --...or anything that shipped in that period - OR ($$||_ship_date||$$ BETWEEN [app_baseline_from_date] AND [app_baseline_to_date]) + OR ($$||_ship_date||$$ BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date) ) --be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships $$::text @@ -132,7 +132,7 @@ $$ FROM fc.live o$$||E'\n'||_perd_joins||$$ WHERE - $$||_order_date||$$ BETWEEN [app_plug_fromdate] AND [app_plug_todate] + $$||_order_date||$$ BETWEEN 'app_plug_fromdate'::date AND 'app_plug_todate'::date --be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships $$ INTO @@ -141,8 +141,9 @@ INTO ------------------------------copy a full year and increment by 1 year for the baseline------------------------- SELECT -$$INSERT INTO - fc.live +--$$INSERT INTO +-- fc.live +$$,incr AS ( SELECT $$||_clist_inc|| $$ @@ -150,9 +151,16 @@ SELECT ,'baseline' iter FROM baseline o$$||E'\n'||_perd_joins||$$ +) +INSERT INTO + fc.live +SELECT + * +FROM + incr i WHERE - $$||_order_date||' >= [app_first_forecast_date]'||$$ - OR $$||_ship_date||' >= [app_first_forecast_date]' + i.$$||_order_date||$$ >= 'app_first_forecast_date'::date$$||$$ + OR i.$$||_ship_date||$$ >= 'app_first_forecast_date'::date$$ --any orders in the forecast period, or any sales in the forecast period (from open orders) INTO _baseline;