commit: 2025-04-08 19:30:33

This commit is contained in:
Paul Trowbridge 2025-04-08 19:30:33 -04:00
parent 2815e25802
commit 76a0f49450
2 changed files with 189 additions and 43 deletions

52
columns Normal file
View File

@ -0,0 +1,52 @@
fspr
plnt
promo
terms
bill_cust_descr
ship_cust_descr
dsm
quota_rep_descr
director
billto_group
shipto_group
chan
chansub
chan_retail
part
part_descr
part_group
branding
majg_descr
ming_descr
majs_descr
mins_descr
segm
substance
fs_line
r_currency
r_rate
c_currency
c_rate
units
value_loc
value_usd
cost_loc
cost_usd
calc_status
flag
order_date
order_month
order_season
request_date
request_month
request_season
ship_date
ship_month
ship_season
version
iter
logid
tag
comment
module
pounds

View File

@ -1,16 +1,32 @@
BEGIN; -- BEGIN;
--only do this when starting fresh --only do this when starting fresh
TRUNCATE TABLE rlarp.osm_pool; -- TRUNCATE TABLE rlarp.osm_pool;
TRUNCATE TABLE rlarp.osm_log CASCADE; -- TRUNCATE TABLE rlarp.osm_log CASCADE;
ALTER SEQUENCE rlarp.osm_log_id_seq RESTART 1; -- ALTER SEQUENCE rlarp.osm_log_id_seq RESTART 1;
--
--
-- DROP TABLE IF EXISTS rlarp.osm_pool_stage;
-- CREATE TABLE rlarp.osm_pool_stage AS (
DROP TABLE IF EXISTS rlarp.osm_pool_stage;
CREATE TABLE rlarp.osm_pool_stage AS (
WITH WITH
repc AS ( -- ,orphans AS (
-- SELECT
-- renamed
-- ,rerep
-- ,pounds
-- FROM
--
-- )
profile AS (
SELECT * FROM rlarp.osm WHERE shipgrp IN (select distinct renamed FROM rlarp.orphans ) AND oseas >= 2018
)
-- SELECT * FROM profile
,checkc AS (
SELECT shipgrp, min(oseas) oseas FROM profile GROUP BY shipgrp
)
-- SELECT * FROM checkc
,repc AS (
SELECT SELECT
LTRIM(RTRIM(C.A9)) RCODE LTRIM(RTRIM(C.A9)) RCODE
,C.A30 REPP ,C.A30 REPP
@ -49,32 +65,32 @@ repc AS (
) )
--select * from seg --select * from seg
----------------------------------------------------sales major codes---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------sales major codes----------------------------------------------------------------------------------------------------------------------------------
,SJ AS ( -- ,SJ AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = '' -- SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') = ''
) -- )
--select * from sj -- --select * from sj
----------------------------------------------------sales minor codes---------------------------------------------------------------------------------------------------------------------------------- -- ----------------------------------------------------sales minor codes----------------------------------------------------------------------------------------------------------------------------------
,SI AS ( -- ,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> '' -- SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
) -- )
--select * from si --select * from si
,logload AS ( ,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','baseline','type','build_pool','stamp',current_timestamp,'source','top level','message','baseline') RETURNING * INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','New Cust','type','orphans','stamp',current_timestamp,'source','top level','message','New Customer') RETURNING *
) )
--select * from logload --select * from logload
,loadset AS ( ,loadset AS (
SELECT SELECT
fspr o.fspr
,plnt ,o.plnt
,promo ,o.promo
,terms ,o.terms
,bill_cust||' - '||bc.bvname bill_cust_descr ,o.bill_cust||' - '||bc.bvname bill_cust_descr
,ship_cust||' - '||sc.bvname ship_cust_descr ,o.ship_cust||' - '||sc.bvname ship_cust_descr
,dsm ,o.dsm
,coalesce(repc.repp,dsm) quota_rep_descr ,coalesce(repc.repp,dsm) quota_rep_descr
,repc.director ,repc.director
,account billto_group ,o.account billto_group
,shipgrp shipto_group ,o.shipgrp shipto_group
,chan ,chan
,chansub ,chansub
,CASE seg.segm ,CASE seg.segm
@ -136,28 +152,33 @@ SELECT
,round(fb_qty,2) units ,round(fb_qty,2) units
,round(fb_val_loc,2) value_loc ,round(fb_val_loc,2) value_loc
,round(fb_val_loc * r_rate,2) value_usd ,round(fb_val_loc * r_rate,2) value_usd
,round(fb_cst_loc_cur,2) cost_loc ,round(fb_qty * i.curstd,2) cost_loc
,round(fb_cst_loc_cur * c_rate,2) cost_usd ,round(fb_qty * i.curstdus,2) cost_usd
,sum(fb_qty * i.nwht * case i.nwun WHEN 'KG' THEN 2.2046 ELSE 1 END) OVER (partition by o.shipgrp) cust_lbs
,fb_qty * i.nwht * case i.nwun WHEN 'KG' THEN 2.2046 ELSE 1 END line_lbs
,calc_status ,calc_status
,flag ,flag
,o.odate order_date ,(odate + ((2026 - o.oseas)||' years')::interval)::date order_date
,to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon') order_month ,to_char(CASE WHEN extract(month FROM o.odate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.odate),'FM00')||' - '||to_char(o.odate,'TMMon') order_month
,oseas order_season ,2026 order_season
,rdate request_date ,(rdate + ((2026 - o.oseas)||' years')::interval)::date request_date
,to_char(CASE WHEN extract(month FROM o.rdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.rdate),'FM00')||' - '||to_char(o.rdate,'TMMon') request_month ,to_char(CASE WHEN extract(month FROM o.rdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.rdate),'FM00')||' - '||to_char(o.rdate,'TMMon') request_month
,rseas request_season ,rseas + (2026 - o.oseas) request_season
,sdate ship_date ,(sdate + ((2026 - o.oseas)||' years')::interval)::date ship_date
,to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon') ship_month ,to_char(CASE WHEN extract(month FROM o.sdate) >= 6 THEN -5 ELSE 7 END + extract(month FROM o.sdate),'FM00')||' - '||to_char(o.sdate,'TMMon') ship_month
,sseas ship_season ,sseas + (2026 - o.oseas) ship_season
,version ,'b26' version
,iter ,'fc orphans' iter
,logload.id logid ,logload.id logid
,logload.doc->>'tag' tag ,logload.doc->>'tag' tag
,logload.doc->>'message' "comment" ,logload.doc->>'message' "comment"
,logload.doc->>'type' module ,logload.doc->>'type' module
,round(o.fb_qty * o.lbs) pounds -- ,round(o.fb_qty * o.lbs) pounds
FROM FROM
rlarp.osmf o checkc p
INNER JOIN rlarp.osm o ON
p.shipgrp = o.shipgrp
AND p.oseas = o.oseas
CROSS JOIN logload CROSS JOIN logload
LEFT OUTER JOIN lgdat.cust bc ON LEFT OUTER JOIN lgdat.cust bc ON
bc.bvcust = o.bill_cust bc.bvcust = o.bill_cust
@ -181,12 +202,85 @@ FROM
seg.glec = o.glec seg.glec = o.glec
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part i.item = o.part
WHERE
iter NOT IN ('py carryover','short ship','bad date')
AND sseas IS NOT NULL
AND odate <= sdate
AND fs_line = '41010'
AND calc_status <> 'CANCELLED'
) )
SELECT * FROM loadset WHERE iter NOT IN ('py carryover','short ship','bad date') ,scale AS (
) WITH DATA; SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,round(units * pounds/cust_lbs,2) units
,round(value_loc * pounds/cust_lbs,2) value_loc
,round(value_usd * pounds/cust_lbs,2) value_usd
,round(cost_loc * pounds/cust_lbs,2) cost_loc
,round(cost_usd * pounds/cust_lbs,2) cost_usd
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,ship_month
,ship_season
,version
,iter
-- ,cust_lbs
,logid
,tag
,comment
,module
,round(line_lbs * pounds/cust_lbs,5) pounds
-- ,pounds/cust_lbs factor
FROM
rlarp.orphans o
INNER JOIN loadset l ON
l.shipto_group = o.renamed
WHERE
cust_lbs >= 1000
)
INSERT INTO rlarp.osm_pool SELECT * FROM scale;
-- select * from scale) WITH DATA;
-- INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
-- SELECT * FROM loadset WHERE iter NOT IN ('py carryover','short ship','bad date')
-- ) WITH DATA;
INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
--CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr); --CREATE INDEX osm_qr ON rlarp.osm_pool(quota_rep_descr, bill_cust_descr, ship_cust_descr);
--ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id); --ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);
@ -194,4 +288,4 @@ INSERT INTO rlarp.osm_pool SELECT * FROM rlarp.osm_pool_stage;
--GRANT ALL ON TABLE rlarp.osm_log TO api; --GRANT ALL ON TABLE rlarp.osm_log TO api;
--GRANT ALL ON TABLE rlarp.osm_pool TO api; --GRANT ALL ON TABLE rlarp.osm_pool TO api;
COMMIT; -- COMMIT;