From 73f7667ef7f1e432582a7535aad818ac59d6987a Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 6 Nov 2020 00:12:05 -0500 Subject: [PATCH] get column list for plu to increment year --- generate_sql/col_baseline.sql | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/generate_sql/col_baseline.sql b/generate_sql/col_baseline.sql index 238d0a1..92b7a9b 100644 --- a/generate_sql/col_baseline.sql +++ b/generate_sql/col_baseline.sql @@ -2,6 +2,7 @@ DO $func$ DECLARE _clist text; + _clist_inc text; _ytdbody text; _order_date text; _ship_date text; @@ -23,6 +24,18 @@ FROM WHERE func NOT IN ('version'); +---------------------------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'); + --RAISE NOTICE 'build list: %',clist; SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_date') INTO _order_date; @@ -60,17 +73,15 @@ INTO ------------------------------------pull a plug from actuals to create a full year baseline------------------ SELECT +$$SELECT + $$||_clist_inc|| $$ ,'baseline' "version" ,'plug' iter FROM rlarp.osm_dev o - LEFT OUTER JOIN gld ON - gld.fspr = o.fspr - LEFT OUTER JOIN gld ss ON - greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' BETWEEN ss.sdat AND ss.edat WHERE - [target_odate] BETWEEN [target_odate_plug_from] AND [target_odate_plug_to] + $$||_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 $$ INTO @@ -79,9 +90,9 @@ INTO ------------------------------stack the sql into the final format------------------------------------------------ SELECT - _ytdbody - ||$$UNION ALL - $$||_actpy + _ytdbody|| +$$UNION ALL +$$||_actpy INTO _sql;