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