initial renaming of columns

This commit is contained in:
pt 2020-02-14 21:16:25 -05:00
parent 7635380ccc
commit 07f69a80ae

View File

@ -9,21 +9,20 @@ WITH
target AS (select target_increment incr) target AS (select target_increment incr)
,testv AS ( ,testv AS (
SELECT SELECT
sum(fb_qty) tot sum(units) tot
,sum(fb_qty) FILTER (WHERE iter = 'copy') base ,sum(units) FILTER (WHERE iter = 'copy') base
,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart ,COALESCE(sum(units) FILTER (WHERE module = 'new basket'),0) newpart
,sum(fb_val_loc *r_rate) totsales ,sum(value_loc *r_rate) totsales
,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales ,sum(value_loc *r_rate) FILTER (WHERE iter = 'copy') basesales
,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales ,COALESCE(sum(value_loc *r_rate) FILTER (WHERE module = 'new basket'),0) newpartsales
FROM FROM
rlarp.osm_fcpool rlarp.osm_pool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
-----------------additional params------------------- -----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND order_date <= ship_date
) )
,flagv AS ( ,flagv AS (
SELECT SELECT
@ -68,6 +67,7 @@ target AS (select target_increment incr)
,to_char(N1FSYP,'FM0000') FSPR ,to_char(N1FSYP,'FM0000') FSPR
,N1SD01 SDAT ,N1SD01 SDAT
,N1ED01 EDAT ,N1ED01 EDAT
,daterange(n1sd01, n1ed01) drange
,to_char(N1ED01,'yymm') CAPR ,to_char(N1ED01,'yymm') CAPR
,N1ED01 - N1SD01 +1 NDAYS ,N1ED01 - N1SD01 +1 NDAYS
,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR ,CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(YEAR FROM N1ED01) + 1 ELSE EXTRACT(YEAR FROM N1ED01) END SSYR
@ -82,21 +82,21 @@ target AS (select target_increment incr)
--AND DIGITS(N1FSYP) = '1901' --AND DIGITS(N1FSYP) = '1901'
) )
,mseq AS ( ,mseq AS (
SELECT * FROM SELECT * FROM
( (
VALUES VALUES
('Jun',1,6,-1) ('01 - Jun',1,6,-1)
,('Jul',2,7,-1) ,('02 - Jul',2,7,-1)
,('Aug',3,8,-1) ,('03 - Aug',3,8,-1)
,('Sep',4,9,-1) ,('04 - Sep',4,9,-1)
,('Oct',5,10,-1) ,('05 - Oct',5,10,-1)
,('Nov',6,11,-1) ,('06 - Nov',6,11,-1)
,('Dec',7,12,-1) ,('07 - Dec',7,12,-1)
,('Jan',8,1,0) ,('08 - Jan',8,1,0)
,('Feb',9,2,0) ,('09 - Feb',9,2,0)
,('Mar',10,3,0) ,('10 - Mar',10,3,0)
,('Apr',11,4,0) ,('11 - Apr',11,4,0)
,('May',12,5,0) ,('12 - May',12,5,0)
) x(m,s,cal,yr) ) x(m,s,cal,yr)
) )
,alldates AS ( ,alldates AS (
@ -105,68 +105,68 @@ target AS (select target_increment incr)
,terms ,terms
,order_month ,order_month
,mseq.s seq ,mseq.s seq
,orderdate ,order_date
,requestdate ,request_date
,shipdate ,ship_date
,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd
FROM FROM
rlarp.osm_fcpool rlarp.osm_pool
LEFT OUTER JOIN mseq ON LEFT OUTER JOIN mseq ON
mseq.m = order_month mseq.m = order_month
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
-----------------additional params------------------- -----------------additional params-------------------
AND version = 'b20' AND version = 'b21'
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' WHEN 'scale new part' THEN module = 'new basket'
END END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND order_date <= ship_date
GROUP BY GROUP BY
promo promo
,terms ,terms
,order_month ,order_month
,mseq.s ,mseq.s
,orderdate ,order_date
,requestdate ,request_date
,shipdate ,ship_date
HAVING HAVING
sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0 sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0
) )
--select * from alldates --select * from alldates
,dom AS ( ,dom AS (
SELECT SELECT
extract(day FROM orderdate) dom extract(day FROM order_date) dom
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
extract(day FROM orderdate) extract(day FROM order_date)
) )
---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)--------------- ---------------------may want ot look at a top-5 mix solution in the future facilitated by sum() over (order by sales desc)---------------
,mmix AS ( ,mmix AS (
SELECT SELECT
to_char(orderdate,'Mon') _month to_char(order_date,'Mon') _month
,seq ,seq
,promo ,promo
,sum(extract(day from orderdate)*value_usd) dom_wa ,sum(extract(day from order_date)*value_usd) dom_wa
--,requestdate-orderdate rlag --,request_date-order_date rlag
,sum((requestdate-orderdate)*(value_usd)) rlag_wa ,sum((request_date-order_date)*(value_usd)) rlag_wa
--,shipdate - requestdate slag --,ship_date - request_date slag
,sum((shipdate - requestdate)*(value_usd)) slag_wa ,sum((ship_date - request_date)*(value_usd)) slag_wa
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
alldates alldates
GROUP BY GROUP BY
to_char(orderdate,'Mon') to_char(order_date,'Mon')
,seq ,seq
,promo ,promo
--,extract(day from orderdate) --,extract(day from order_date)
--,requestdate-orderdate --,request_date-order_date
--,shipdate - requestdate --,ship_date - request_date
) )
,targm AS (select s, m from mseq where m = 'target_month' ) ,targm AS (select s, m from mseq where m = 'target_month' )
,mmixp AS ( ,mmixp AS (
@ -255,20 +255,20 @@ SELECT
,0::numeric(15,5) ddqtsi --0 ,0::numeric(15,5) ddqtsi --0
,0::numeric(15,5) fgqshp --0 ,0::numeric(15,5) fgqshp --0
,0::numeric(15,5) diqtsh --0 ,0::numeric(15,5) diqtsh --0
,sum(coalesce(fb_qty,0)) fb_qty --history value ,sum(coalesce(units,0)) units --history value
,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix ,sum(coalesce(fb_cst_loc,0)) fb_cst_loc --history part mix
,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data ,sum(coalesce(fb_cst_loc_cur,0)) fb_cst_loc_cur --master data
,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data ,sum(coalesce(fb_cst_loc_fut,0)) fb_cst_loc_fut --master data
,sum(coalesce(fb_val_loc,0)) fb_val_loc --history value ,sum(coalesce(value_loc,0)) value_loc --history value
,sum(coalesce(fb_val_loc_pl,0)) fb_val_loc_pl --0 ,sum(coalesce(value_loc_pl,0)) value_loc_pl --0
,calc_status --0 ,calc_status --0
,flag --0 ,flag --0
,null::date orderdate --history date mix ,null::date order_date --history date mix
,null::date requestdate --history date mix ,null::date request_date --history date mix
,null::date shipdate --history date mix ,null::date ship_date --history date mix
,null::date adj_orderdate --history ,null::date order_date --history
,null::date adj_requestdate --history ,null::date adj_request_date --history
,null::date adj_shipdate --history ,null::date ship_date --history
---------------ui columns------------------------- ---------------ui columns-------------------------
,null::numeric order_season ,null::numeric order_season
,null::text order_month ,null::text order_month
@ -300,7 +300,7 @@ SELECT
,null cost_usd ,null cost_usd
,null units ,null units
FROM FROM
rlarp.osm_fcpool rlarp.osm_pool
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
scenario = target_scenario scenario = target_scenario
@ -308,10 +308,10 @@ SELECT
AND CASE (SELECT flag FROM flagv) AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter = 'copy' WHEN 'scale copy' THEN iter = 'copy'
WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' WHEN 'scale new part' THEN module = 'new basket'
END END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND adj_orderdate <= adj_shipdate AND order_date <= ship_date
GROUP BY GROUP BY
plnt ---master data plnt ---master data
,terms ,terms
@ -376,8 +376,8 @@ SELECT
,scale AS ( ,scale AS (
SELECT SELECT
(SELECT incr::numeric FROM target) incr (SELECT incr::numeric FROM target) incr
,(SELECT sum(fb_qty) FROM basemix) base ,(SELECT sum(units) FROM basemix) base
,(SELECT incr::numeric FROM target)/(SELECT sum(fb_qty) FROM basemix) factor ,(SELECT incr::numeric FROM target)/(SELECT sum(units) FROM basemix) factor
) )
,final AS ( ,final AS (
SELECT SELECT
@ -441,20 +441,20 @@ SELECT
,b.ddqtsi --0 ,b.ddqtsi --0
,b.fgqshp --0 ,b.fgqshp --0
,b.diqtsh --0 ,b.diqtsh --0
,b.fb_qty*s.factor*m.momix fb_qty ,b.units*s.factor*m.momix units
,b.fb_cst_loc*s.factor*m.momix fb_cst_loc ,b.fb_cst_loc*s.factor*m.momix fb_cst_loc
,b.fb_cst_loc_cur*s.factor*m.momix fb_cst_loc_cur ,b.fb_cst_loc_cur*s.factor*m.momix fb_cst_loc_cur
,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut ,b.fb_cst_loc_fut*s.factor*m.momix fb_cst_loc_fut
,b.fb_val_loc*s.factor*m.momix fb_val_loc ,b.value_loc*s.factor*m.momix value_loc
,b.fb_val_loc_pl*s.factor*m.momix fb_val_loc_pl ,b.value_loc_pl*s.factor*m.momix value_loc_pl
,b.calc_status --0 ,b.calc_status --0
,b.flag --0 ,b.flag --0
,make_date(mseq.yr + 2020,mseq.cal,m.odom) orderdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag requestdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag request_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag shipdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) adj_orderdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) order_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_requestdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag adj_request_date
,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag adj_shipdate ,make_date(mseq.yr + 2020,mseq.cal,m.odom) + rlag + slag ship_date
,'replace_version' "version" --calculated ,'replace_version' "version" --calculated
,'replace_source'||' volume' iter --calculated ,'replace_source'||' volume' iter --calculated
-----------------------ui columns-------------------------------- -----------------------ui columns--------------------------------
@ -484,11 +484,11 @@ SELECT
,b.mod_chansub ,b.mod_chansub
,b.quota_rep_descr ,b.quota_rep_descr
,b.director_descr ,b.director_descr
,(b.fb_val_loc*s.factor*m.momix)::numeric value_loc ,(b.value_loc*s.factor*m.momix)::numeric value_loc
,(b.fb_val_loc*s.factor*m.momix*r_rate)::numeric value_usd ,(b.value_loc*s.factor*m.momix*r_rate)::numeric value_usd
,(b.fb_cst_loc*s.factor*m.momix)::numeric cost_loc ,(b.fb_cst_loc*s.factor*m.momix)::numeric cost_loc
,(b.fb_cst_loc*s.factor*m.momix*c_rate)::numeric cost_usd ,(b.fb_cst_loc*s.factor*m.momix*c_rate)::numeric cost_usd
,(b.fb_qty*s.factor*m.momix)::numeric units ,(b.units*s.factor*m.momix)::numeric units
FROM FROM
basemix b basemix b
CROSS JOIN scale s CROSS JOIN scale s
@ -506,7 +506,7 @@ WHERE
m._month = (SELECT _month FROM closest) m._month = (SELECT _month FROM closest)
) )
, ins AS ( , ins AS (
INSERT INTO rlarp.osm_fcpool SELECT * FROM final RETURNING * INSERT INTO rlarp.osm_pool SELECT * FROM final RETURNING *
) )
,insagg AS ( ,insagg AS (
SELECT SELECT