Update some 2022 references to 2024, and fix round() function calls.

This commit is contained in:
PhilRunninger 2023-04-05 12:37:35 -04:00
parent bbac2ec390
commit 480adef789
5 changed files with 95 additions and 94 deletions

View File

@ -1,4 +1,4 @@
WITH
WITH
/*
the volume must be expressed in terms of units, since that is what it will be scaling
*/
@ -55,7 +55,7 @@ target AS (select target_volume vincr, target_price pincr)
testv
)
,GLD AS MATERIALIZED (
SELECT
SELECT
N1COMP COMP
,N1CCYY FSYR
,KPMAXP PERDS
@ -65,19 +65,19 @@ target AS (select target_volume vincr, target_price pincr)
,N1ED01 EDAT
,daterange(n1sd01, n1ed01,'[]') drange
,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
,to_char(CASE WHEN EXTRACT(MONTH FROM N1ED01) >= 6 THEN EXTRACT(MONTH FROM N1ED01) -5 ELSE EXTRACT(MONTH FROM N1ED01) +7 END,'FM00') SSPR
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
FROM
LGDAT.GLDATREF
INNER JOIN LGDAT.GLDATE ON
KPCOMP = N1COMP AND
KPCCYY = N1CCYY
WHERE
N1COMP = 93
)
,mseq AS (
SELECT * FROM
SELECT * FROM
(
VALUES
('01 - Jun',1,6,-1)
@ -95,7 +95,7 @@ target AS (select target_volume vincr, target_price pincr)
) x(m,s,cal,yr)
)
,alldates AS MATERIALIZED(
SELECT
SELECT
promo
,terms
,order_month
@ -112,7 +112,7 @@ target AS (select target_volume vincr, target_price pincr)
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
WHEN 'scale new part' THEN module = 'new basket'
@ -177,13 +177,13 @@ target AS (select target_volume vincr, target_price pincr)
mmix
)
,closest AS (
SELECT
SELECT
_month, targm.s, m
FROM
mmixp
FROM
mmixp
CROSS JOIN targm
ORDER BY
abs(seq - targm.s) ASC
ORDER BY
abs(seq - targm.s) ASC
LIMIT 1
)
---------------------the role of basemix here is to get non-dated info which is then dated in the next step---------------------
@ -234,7 +234,7 @@ SELECT
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
WHEN 'scale new part' THEN module = 'new basket'
@ -323,15 +323,15 @@ SELECT
,round(b.cost_usd*s.factor*m.momix ,2) cost_usd
,b.calc_status
,b.flag
,make_date(mseq.yr + 2022,mseq.cal,m.odom) order_date
,od.sspr || ' - ' || to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom),'Mon') order_month
,make_date(mseq.yr + 2024,mseq.cal,m.odom) order_date
,od.sspr || ' - ' || to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom),'Mon') order_month
,od.ssyr order_season
,make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag request_date
,rd.sspr || ' - ' ||to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag,'Mon') request_month
,make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag request_date
,rd.sspr || ' - ' ||to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag,'Mon') request_month
,rd.ssyr request_season
,make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag ship_date
,sd.sspr || ' - ' || to_char(make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
,sd.ssyr ship_season
,make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag ship_date
,sd.sspr || ' - ' || to_char(make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag,'Mon') ship_month
,sd.ssyr ship_season
,'replace_version' "version"
,'replace_source'||' volume' iter
,log.id
@ -347,11 +347,11 @@ FROM
LEFT OUTER JOIN mseq ON
mseq.m = closest.m
LEFT OUTER JOIN gld od ON
make_date(mseq.yr + 2022,mseq.cal,m.odom) BETWEEN od.sdat AND od.edat
make_date(mseq.yr + 2024,mseq.cal,m.odom) BETWEEN od.sdat AND od.edat
LEFT OUTER JOIN gld rd ON
make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag BETWEEN rd.sdat AND rd.edat
make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag BETWEEN rd.sdat AND rd.edat
LEFT OUTER JOIN gld sd ON
make_date(mseq.yr + 2022,mseq.cal,m.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
make_date(mseq.yr + 2024,mseq.cal,m.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat
WHERE
m._month = (SELECT _month FROM closest)
)
@ -362,7 +362,7 @@ WHERE
,CASE WHEN (SELECT sum(value_loc * r_rate) FROM volume) = 0 THEN
0
ELSE
((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume))/(SELECT sum(value_loc * r_rate) FROM volume)
((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume))/(SELECT sum(value_loc * r_rate) FROM volume)
END factor
,CASE WHEN (SELECT sum(value_loc * r_rate) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) FROM volume)) <> 0 THEN
@ -421,7 +421,7 @@ SELECT
,b.request_season
,b.ship_date
,b.ship_month
,b.ship_season
,b.ship_season
,'replace_version' "version"
,'replace_source'||' price' iter
,log.id

View File

@ -19,13 +19,13 @@ FROM
LEFT OUTER JOIN rlarp.osm_log ON
id = logid
WHERE
order_season = 2022
order_season = 2024
GROUP BY
director
,quota_rep_descr
,part
,bill_cust_descr
,ship_cust_descr
,bill_cust_descr
,ship_cust_descr
,order_month
,doc->>'stamp'
,logid
@ -43,7 +43,7 @@ SELECT
,(max(stamp) FILTER (WHERE tag <> 'Initial Build'))::timestamptz "last edit"
FROM
preagg
GROUP BY
GROUP BY
GROUPING SETS (
(director,quota_rep_descr),
(director),

View File

@ -1,3 +1,4 @@
-- Connection: usmidsap01.ubm
WITH
/*
the volume must be expressed in terms of units, since that is what it will be scaling
@ -251,13 +252,13 @@ SELECT
,substring(jr.part_descr,1,8) part_group
,mxm.qty*mxm.momix*jr.mix units
,mxm.amount*mxm.momix*jr.mix value_usd --assume that target dollars are USD
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) order_date
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) order_date
,od.sortmo order_month
,od.ssyr order_season
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag request_date
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag request_date
,rd.sortmo request_month
,rd.ssyr request_season
,make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag + slag ship_date
,make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag + slag ship_date
,sd.sortmo ship_month
,sd.ssyr ship_season
,jr.mix
@ -267,11 +268,11 @@ SELECT
JOIN LATERAL json_to_record(ae.e) as jr(part_descr text, bill_cust_descr text, ship_cust_descr text, mix numeric) ON true
CROSS JOIN mxm
LEFT OUTER JOIN gld od ON
od.drange @> make_date(mxm.yr + 2022,mxm.cal,mxm.odom)
od.drange @> make_date(mxm.yr + 2024,mxm.cal,mxm.odom)
LEFT OUTER JOIN gld rd ON
rd.drange @> (make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag)
rd.drange @> (make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag)
LEFT OUTER JOIN gld sd ON
sd.drange @> (make_date(mxm.yr + 2022,mxm.cal,mxm.odom) + rlag + slag)
sd.drange @> (make_date(mxm.yr + 2024,mxm.cal,mxm.odom) + rlag + slag)
)
--SELECT * FROM basemix
,log AS (
@ -329,11 +330,11 @@ SELECT
,rx.rate r_rate --master data
,copr.curr c_currency --master data
,cx.rate c_rate --master data
,round(b.units ,2) units
,round(b.value_usd / COALESCE(rx.rate,1) ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
,round(b.value_usd ,2) value_usd --b.value is already denominated in usd
,round(COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units ,2) cost_loc
,round(COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate,2) cost_usd
,round(b.units ,2) units
,round((b.value_usd / COALESCE(rx.rate,1))::numeric ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
,round(b.value_usd ,2) value_usd --b.value is already denominated in usd
,round((COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units)::numeric ,2) cost_loc
,round((COALESCE(im.cgstcs,ip.chstcs, ir.y0stcs)*b.units*cx.rate)::numeric,2) cost_usd
,'CLOSED' calc_status
,'SHIPMENT' flag
,b.order_date
@ -423,13 +424,13 @@ FROM
LEFT OUTER JOIN copr ON
copr.comp = yacomp::text
LEFT OUTER JOIN rlarp.ffcret cx ON
cx.perd = '2101'
AND cx.rtyp = 'BG'
cx.perd = '2310'
AND cx.rtyp = 'MA'
AND cx.fcur = copr.curr
AND cx.tcur = 'US'
LEFT OUTER JOIN rlarp.ffcret rx ON
rx.perd = '2101'
AND rx.rtyp = 'BG'
rx.perd = '2310'
AND rx.rtyp = 'MA'
AND rx.fcur = COALESCE(bc.bvcurr,b.r_currency)
AND rx.tcur = 'US'
)

View File

@ -1,4 +1,4 @@
WITH
WITH
target AS (select target_vol vincr, target_prc pincr)
,testv AS (
SELECT
@ -39,17 +39,17 @@ target AS (select target_vol vincr, target_prc pincr)
,basemix AS (
SELECT
fspr
,plnt ---master data
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,quota_rep_descr --master data
,director
,billto_group --master data
,billto_group --master data
,shipto_group
,chan --master data
,chan --master data
,chansub
,chan_retail
,part
@ -62,18 +62,18 @@ target AS (select target_vol vincr, target_prc pincr)
,mins_descr
,segm
,substance
,fs_line --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,r_rate --master data
,c_currency --master data
,c_rate --master data
,sum(coalesce(units,0)) units --history value
,sum(coalesce(value_loc,0)) value_loc --history value
,sum(coalesce(value_usd,0)) value_usd --0
,sum(coalesce(value_usd,0)) value_usd --0
,sum(coalesce(cost_loc,0)) cost_loc --history part mix
,sum(coalesce(cost_usd,0)) cost_usd
,calc_status --0
,flag --0
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
@ -88,9 +88,9 @@ target AS (select target_vol vincr, target_prc pincr)
WHERE
-----------------scenario----------------------------
where_clause
AND ship_season = 2022
AND ship_season = 2024
-----------------additional params-------------------
AND CASE (SELECT flag FROM flagv)
AND CASE (SELECT flag FROM flagv)
WHEN 'scale all' THEN true
WHEN 'scale copy' THEN iter IN ('plan','diff','copy')
WHEN 'scale new part' THEN module = 'new basket'
@ -99,17 +99,17 @@ target AS (select target_vol vincr, target_prc pincr)
AND order_date <= ship_date
GROUP BY
fspr
,plnt ---master data
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,quota_rep_descr --master data
,director
,billto_group --master data
,billto_group --master data
,shipto_group
,chan --master data
,chan --master data
,chansub
,chan_retail
,part
@ -122,13 +122,13 @@ target AS (select target_vol vincr, target_prc pincr)
,mins_descr
,segm
,substance
,fs_line --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,calc_status --0
,flag --0
,r_rate --master data
,c_currency --master data
,c_rate --master data
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
@ -152,17 +152,17 @@ target AS (select target_vol vincr, target_prc pincr)
,volume AS (
SELECT
fspr
,plnt ---master data
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,quota_rep_descr --master data
,director
,billto_group --master data
,billto_group --master data
,shipto_group
,chan --master data
,chan --master data
,chansub
,chan_retail
,part
@ -175,18 +175,18 @@ target AS (select target_vol vincr, target_prc pincr)
,mins_descr
,segm
,substance
,fs_line --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,r_rate --master data
,c_currency --master data
,c_rate --master data
,round(units*s.factor ,2) units
,round(value_loc*s.factor,2) value_loc
,round(value_usd*s.factor,2) value_usd
,round(cost_loc*s.factor ,2) cost_loc
,round(cost_usd*s.factor ,2) cost_usd
,calc_status --0
,flag --0
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
@ -233,17 +233,17 @@ FROM
,pricing AS (
SELECT
fspr
,plnt ---master data
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,quota_rep_descr --master data
,director
,billto_group --master data
,billto_group --master data
,shipto_group
,chan --master data
,chan --master data
,chansub
,chan_retail
,part
@ -256,18 +256,18 @@ FROM
,mins_descr
,segm
,substance
,fs_line --master data
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,r_rate --master data
,c_currency --master data
,c_rate --master data
,0::numeric units
,round((CASE WHEN s.factor = 0 THEN b.units * s.mod_price/b.r_rate ELSE b.value_loc*s.factor END)::numeric,2) value_loc
,round((CASE WHEN s.factor = 0 THEN b.units * s.mod_price ELSE b.value_usd*s.factor END)::numeric ,2) value_usd
,0::numeric cost_loc
,0::numeric cost_usd
,calc_status --0
,flag --0
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season

View File

@ -37,7 +37,7 @@ FROM
WHERE
where_clause
--quota_rep_descr = 'MATTHEW STAAL'
AND order_season IN (2019,2024)
AND order_season IN (2023,2024)
GROUP BY
order_season
,order_month
@ -95,11 +95,11 @@ GROUP BY
SELECT
order_month
,seq
,SUM(units) FILTER (WHERE order_season = 2019) "2023 qty"
,SUM(units) FILTER (WHERE order_season = 2023) "2023 qty"
,SUM(units) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base qty"
,SUM(units) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj qty"
,SUM(units) FILTER (WHERE order_season = 2024) "2024 tot qty"
,SUM(value_usd) FILTER (WHERE order_season = 2019) "2023 value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2023) "2023 value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2024) "2024 tot value_usd"