From 4128e12848ea33f66529ab626a069d18844db97a Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 28 Apr 2022 01:13:13 -0400 Subject: [PATCH] move scale route sql to setup --- routes/scale/generate_route_sql.sh | 2 +- routes/scale/scale.sql | 157 ------------------ .../05_gen_scale.sql | 0 3 files changed, 1 insertion(+), 158 deletions(-) delete mode 100644 routes/scale/scale.sql rename routes/scale/gen_scale.sql => setup_sql/05_gen_scale.sql (100%) diff --git a/routes/scale/generate_route_sql.sh b/routes/scale/generate_route_sql.sh index 3506ac9..2443716 100755 --- a/routes/scale/generate_route_sql.sh +++ b/routes/scale/generate_route_sql.sh @@ -1,4 +1,4 @@ # execure the sql for scale which builds the sql and inserts into a table -$PG -f ./routes/scale/gen_scale.sql +#$PG -f ./routes/scale/gen_scale.sql # pull the sql out of the table and write it to route directory $PG -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o ./routes/scale/scale.sql diff --git a/routes/scale/scale.sql b/routes/scale/scale.sql deleted file mode 100644 index 8bc556e..0000000 --- a/routes/scale/scale.sql +++ /dev/null @@ -1,157 +0,0 @@ -WITH -req AS (SELECT $$app_req$$::jsonb j) -,target AS ( - SELECT - (req.j->>'app_vincr')::numeric vincr --volume - ,(req.j->>'app_pincr')::numeric pincr --price - FROM - req -) ------this block is supposed to test for new products that might not be in baseline etc------- -,test AS ( - SELECT - sum(app_units) FILTER (WHERE app_iter <> 'ACTUALS') total - ,sum(app_units) FILTER (WHERE app_iter = 'baseline') base - FROM - tpsv.dcard o - WHERE - app_where -) -,ilog AS ( - INSERT INTO - fc.log (doc) - SELECT - req.j - FROM - req - RETURNING * -) -------need to group basemix and forego any detail iterations -,basemix AS ( -SELECT - o.id - ,o.logid - ,o."Trans. Date" - ,o."Post Date" - ,o."Description" - ,sum(o."Amount") AS "Amount" - ,o."Category" - ,o."Party" - ,o."Reason" - ,sum(o.app_units) AS app_units - ,'app_forecast_name' AS app_version - ,'scale' AS app_iter - ,0::bigint AS app_logid -FROM - tpsv.dcard o -WHERE - app_where -GROUP BY - o.id - ,o.logid - ,o."Trans. Date" - ,o."Post Date" - ,o."Description" - ,o."Category" - ,o."Party" - ,o."Reason" -), -vscale AS ( - SELECT - (SELECT vincr FROM target) AS target_increment - ,sum(app_units) AS units - ,CASE WHEN sum(app_units) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum(app_units) END AS factor - FROM - basemix -) -,volume_only AS ( -SELECT - o.id - ,o.logid - ,o."Trans. Date" - ,o."Post Date" - ,o."Description" - ,round(o."Amount" * vscale.factor,2) AS "Amount" - ,o."Category" - ,o."Party" - ,o."Reason" - ,round(o.app_units * vscale.factor,5) AS app_units - ,'app_forecast_name' AS app_version - ,'scale volume' AS app_iter - ,(SELECT id FROM ilog) AS app_logid -FROM - basemix o - CROSS JOIN vscale -WHERE - vscale.factor <> 0 -) -,volume AS ( - SELECT * FROM volume_only - UNION ALL - SELECT * FROM basemix WHERE (SELECT factor FROM vscale) = 0 -) -,pscale AS ( -SELECT - (SELECT pincr FROM target) AS target_increment - ,sum("Amount") AS value - ,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN - --if the base value is -0- scaling will not work, need to generate price, factor goes to -0- - 0 - ELSE - CASE WHEN (SELECT factor FROM vscale) = 0 - --if the incoming volume dataset has just basemix data, then create an absolute factor and multiply by basemix - THEN (SELECT pincr FROM target)/(SELECT sum("Amount") FROM volume) - --if the incoming volume table already has an adjusment, just need to create a true-up factor - --multiplying by the raw factor without adding 1 will work since this data set will be added to the volume data set - ELSE ((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") FROM volume) - END - END factor - ,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN - CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") FROM volume)) <> 0 THEN - --if the base value is -0- but the target value hasn't been achieved, derive a price to apply - ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") FROM volume))/(SELECT sum(app_units) FROM volume) - ELSE - 0 - END - ELSE - 0 - END mod_price -FROM - volume -) -,pricing AS ( -SELECT - o.id - ,o.logid - ,o."Trans. Date" - ,o."Post Date" - ,o."Description" - ,round((CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric,2) AS "Amount" - ,o."Category" - ,o."Party" - ,o."Reason" - ,0::numeric AS app_units - ,'app_forecast_name' AS app_version - ,'scale price' AS app_iter - ,(SELECT id FROM ilog) AS app_logid -FROM - volume o - CROSS JOIN pscale -WHERE - pscale.factor <> 0 or pscale.mod_price <> 0 -) -, ins AS ( -INSERT INTO - tpsv.dcard -SELECT - * -FROM - volume_only -UNION ALL -SELECT - * -FROM - pricing -RETURNING * -) -SELECT COUNT(*) as num_rows FROM ins diff --git a/routes/scale/gen_scale.sql b/setup_sql/05_gen_scale.sql similarity index 100% rename from routes/scale/gen_scale.sql rename to setup_sql/05_gen_scale.sql