use target cte to hold targets instead of build sql in node

This commit is contained in:
Paul Trowbridge 2020-12-08 00:49:04 -05:00
parent 33f5453671
commit 1d6317df1e
4 changed files with 40 additions and 16 deletions

View File

@ -121,9 +121,9 @@ server.get('/scale', bodyParser.json(), function(req, res) {
console.log(JSON.stringify(req.body)); console.log(JSON.stringify(req.body));
//parse the where clause into the main sql statement //parse the where clause into the main sql statement
//sql = sql.replace(new RegExp("where_clause", 'g'), w) //sql = sql.replace(new RegExp("where_clause", 'g'), w)
sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr); //sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr);
sql = sql.replace(new RegExp("app_req", 'g'), app_req); sql = sql.replace(new RegExp("app_req", 'g'), app_req);
sql = sql.replace(new RegExp("app_vincr", 'g'), app_vincr); //sql = sql.replace(new RegExp("app_vincr", 'g'), app_vincr);
sql = sql.replace(new RegExp("app_where", 'g'), w); sql = sql.replace(new RegExp("app_where", 'g'), w);
//execute the sql and send the result //execute the sql and send the result
args.push(req.body.app_baseline_from_date); args.push(req.body.app_baseline_from_date);

View File

@ -77,7 +77,14 @@ WHERE
SELECT SELECT
---------$$app_req$$ will hold the request body-------------------- ---------$$app_req$$ will hold the request body--------------------
$$WITH $$WITH
req AS (SELECT $$||'$$app_req$$::jsonb)'||$$ req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
,target AS (
SELECT
(req.j->>'vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price
FROM
req
)
-----this block is supposed to test for new products that might not be in baseline etc------- -----this block is supposed to test for new products that might not be in baseline etc-------
,test AS ( ,test AS (
SELECT SELECT
@ -98,9 +105,9 @@ WHERE
), ),
vscale AS ( vscale AS (
SELECT SELECT
app_vincr AS target_increment (SELECT vincr FROM target) AS target_increment
,sum($$||_units_col||') AS units'||$$ ,sum($$||_units_col||') AS units'||$$
,app_vincr/sum($$||_units_col||$$) AS factor ,(SELECT vincr FROM target)/sum($$||_units_col||$$) AS factor
FROM FROM
basemix basemix
) )
@ -113,19 +120,19 @@ FROM
) )
,pscale AS ( ,pscale AS (
SELECT SELECT
app_pincr AS target_increment (SELECT pincr FROM target) AS target_increment
,sum($$||_value_col||') AS value'||$$ ,sum($$||_value_col||') AS value'||$$
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0- --if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
0 0
ELSE ELSE
--if the target dollar value still does not match the target increment, make this adjustment --if the target dollar value still does not match the target increment, make this adjustment
((SELECT pincr::numeric FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume) ((SELECT pincr FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume)
END factor END factor
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) FROM volume)) <> 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) FROM volume)) <> 0 THEN
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply --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($$||_value_col||$$) FROM volume))/(SELECT sum(units) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_units_col||$$) FROM volume)
ELSE ELSE
0 0
END END
@ -138,6 +145,11 @@ FROM
,pricing AS ( ,pricing AS (
SELECT SELECT
$$||_clist_prc||$$ $$||_clist_prc||$$
FROM
volume o
CROSS JOIN pscale
WHERE
pscale.factor <> 0 or pscale.mod_price <> 0
) )
INSERT INTO INSERT INTO
fc.live fc.live

View File

@ -1,4 +1,4 @@
# execure the sql for scale which builds the sql and inserts into a table # execure the sql for scale which builds the sql and inserts into a table
$PGD -f routes/scale/gen_scale.sql $PGD -f gen_scale.sql
# pull the sql out of the table and write it to route directory # pull the sql out of the table and write it to route directory
$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o routes/scale/scale_final.sql $PGD -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o scale.sql

View File

@ -1,5 +1,12 @@
WITH WITH
req AS (SELECT $$app_req$$::jsonb) req AS (SELECT $$app_req$$::jsonb j)
,target AS (
SELECT
(req.j->>'vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price
FROM
req
)
-----this block is supposed to test for new products that might not be in baseline etc------- -----this block is supposed to test for new products that might not be in baseline etc-------
,test AS ( ,test AS (
SELECT SELECT
@ -120,9 +127,9 @@ WHERE
), ),
vscale AS ( vscale AS (
SELECT SELECT
app_vincr AS target_increment (SELECT vincr FROM target) AS target_increment
,sum(fb_qty) AS units ,sum(fb_qty) AS units
,app_vincr/sum(fb_qty) AS factor ,(SELECT vincr FROM target)/sum(fb_qty) AS factor
FROM FROM
basemix basemix
) )
@ -235,19 +242,19 @@ FROM
) )
,pscale AS ( ,pscale AS (
SELECT SELECT
app_pincr AS target_increment (SELECT pincr FROM target) AS target_increment
,sum(fb_val_loc) AS value ,sum(fb_val_loc) AS value
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0- --if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
0 0
ELSE ELSE
--if the target dollar value still does not match the target increment, make this adjustment --if the target dollar value still does not match the target increment, make this adjustment
((SELECT pincr::numeric FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume) ((SELECT pincr FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume)
END factor END factor
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume)) <> 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume)) <> 0 THEN
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply --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(fb_val_loc) FROM volume))/(SELECT sum(units) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_qty) FROM volume)
ELSE ELSE
0 0
END END
@ -360,6 +367,11 @@ SELECT
,o.rseas ,o.rseas
,o.sdate ,o.sdate
,o.sseas ,o.sseas
FROM
volume o
CROSS JOIN pscale
WHERE
pscale.factor <> 0 or pscale.mod_price <> 0
) )
INSERT INTO INSERT INTO
fc.live fc.live