From fee40dcb87cfea15abcb48d0a4b6116141120832 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 12 Jan 2021 15:08:08 -0500 Subject: [PATCH] need BDL pricing to connect to SLV default parts, work on creating some type of bridge --- db_schema/extract_for_customer_facing.pg.sql | 286 ++++++++++--------- 1 file changed, 146 insertions(+), 140 deletions(-) diff --git a/db_schema/extract_for_customer_facing.pg.sql b/db_schema/extract_for_customer_facing.pg.sql index 1f50d92..e1989e0 100644 --- a/db_schema/extract_for_customer_facing.pg.sql +++ b/db_schema/extract_for_customer_facing.pg.sql @@ -1,8 +1,20 @@ --EXPLAIN (ANALYZE, BUFFERS) WITH -pc AS materialized ( +-------------make BDL and SLV the same thing------------------------------------------------------- +-------------assumes that left column has all unique packages in rlarp.pcore----------------------- +vu AS ( + SELECT * FROM ( VALUES + ('BDL','SLV'), + ('BDL','BDL'), + ('PLT','PLT'), + ('CSE','CSE') + ) x(bdl,uom) +) +----------------add columns for a better understanding of what is otherwise implied---------------- +,pc AS materialized ( SELECT - STLC + plist + ,STLC ,COALESCE(COLTIER,'') COLTIER ,coalesce(BRANDING,'' ) branding ,COALESCE(ACCS ,'') ACCS @@ -25,30 +37,17 @@ pc AS materialized ( FROM rlarp.PCORE P ) +--------------list unique colors per line----------------------------------------------------------- ,colors AS ( SELECT - pc.STLC + pc.plist + ,pc.STLC ,pc.COLTIER ,pc.branding ,pc.ACCS ,pc.SUFFIX ,string_agg( DISTINCT ltrim(rtrim(i.COLC)),', ') tclist ,jsonb_agg( DISTINCT ltrim(rtrim(i.COLC))) clist - ------price 1 ----------------- - --,pc.PCKG1 - --,pc.VOLL1 - --,pc.VOL1_UOM - --,pc.VOL1_PRICE - ------price 2 ----------------- - --,pc.PCKG2 - --,pc.VOLl2 - --,pc.VOL2_UOM - --,pc.VOL2_PRICE - ------price 3 ----------------- - --,pc.PCKG1 - --,pc.VOLL3 - --,pc.VOL3_UOM - --,pc.VOL3_PRICE FROM pc LEFT OUTER JOIN RLARP.ITEMMv i ON @@ -60,32 +59,19 @@ FROM --AND COALESCE(i.SUFFIX,'') = pc.suffix AND APLNT <> 'I' GROUP BY - pc.STLC + pc.plist + ,pc.STLC ,pc.COLTIER ,pc.branding ,pc.ACCS ,pc.SUFFIX - ----price 1 ----------------- - ,pc.PCKG1 - ,pc.VOLL1 - ,pc.VOL1_UOM - ,pc.VOL1_PRICE - ----price 2 ----------------- - ,pc.PCKG2 - ,pc.VOLl2 - ,pc.VOL2_UOM - ,pc.VOL2_PRICE - ----price 3 ----------------- - ,pc.PCKG1 - ,pc.VOLL3 - ,pc.VOL3_UOM - ,pc.VOL3_PRICE ) -----------since joining to the item master will be differnt for the last bulk columm--------------- -----------must first pivot the data out then join based on the type of row------------------------- ,unpivot AS ( - SELECT - pc.stlc + SELECT + pc.plist + ,pc.stlc ,pc.coltier ,pc.branding ,pc.accs @@ -97,9 +83,12 @@ GROUP BY ,pc.vol1_price price FROM pc + WHERE + pc.vol1_price IS NOT null UNION SELECT - pc.stlc + pc.plist + ,pc.stlc ,pc.coltier ,pc.branding ,pc.accs @@ -111,9 +100,12 @@ GROUP BY ,pc.vol2_price price FROM pc + WHERE + pc.vol2_price IS NOT null UNION SELECT - pc.stlc + pc.plist + ,pc.stlc ,pc.coltier ,pc.branding ,pc.accs @@ -125,11 +117,15 @@ GROUP BY ,pc.vol3_price price FROM pc + WHERE + pc.vol3_price IS NOT null ) -----link to actual part numbers, tag the sequence so a single item can be chosen----- +--SELECT DISTINCT pckg FROM unpivot; +--link to actual part numbers, tag the sequence so a single item can be chosen----- ,items_init AS ( SELECT - up.STLC + up.plist + ,up.stlc ,up.coltier ,up.branding ,up.accs @@ -140,115 +136,125 @@ SELECT ,up.vol_uom ,up.voL_qty ,up.price - ,row_number() OVER (PARTITION BY up.stlc, up.coltier, up.branding, up.accs, up.suffix, up.pckg, up.col, up.vol_uom ORDER BY item DESC) seq + ,row_number() OVER (PARTITION BY up.plist, up.stlc, up.coltier, up.branding, up.accs, up.suffix, up.pckg, up.col, up.vol_uom ORDER BY item ASC) seq ,item --subquery option is not performant at all - --,( - -- SELECT - -- item - -- FROM - -- rlarp.itemmv v - -- WHERE - -- v.stlc = up.stlc - -- AND v.branding = up.branding - -- AND v.accs = up.accs - -- AND v.suffix = up.suffix - -- AND v.uomp ? up.pckg - -- AND v.aplnt <> 'I' - -- LIMIT 1 - --) item_subq FROM unpivot up + ----make all the BDL units of measure BDL and also SLV with intentional duplication + ----becusae some defaults units on itemm have SLV and not BDL, but still need a join + LEFT OUTER JOIN vu ON + vu.bdl = up.pckg LEFT OUTER JOIN RLARP.ITEMMv i ON i.STLC = up.stlc --AND i.COLTIER = pc.coltier - AND i.uomp ? up.pckg + AND i.uomp ? vu.uom AND COALESCE(i.BRANDING,'') = up.branding AND COALESCE(i.ACCS,'') = up.ACCS AND COALESCE(i.SUFFIX,'') = up.suffix AND APLNT <> 'I' ) -----compile all the conversions into a single json object---------------------- -,items AS ( - SELECT - jsonb_agg( DISTINCT - jsonb_build_object( - 'partn', item - ,'fu', vol_uom - ,'tu','PC' - ) - ) conv - FROM - items_init - WHERE - seq = 1 - AND COALESCE(item,'') <> '' -) ---do the conversion------------------------------------------------------------- -, conversion AS ( - SELECT - rslt.* - FROM - items - JOIN LATERAL rlarp.uom_array(conv) rslt ON true -) ---missing column 3 ---SELECT * FROM unpivot ORDER BY stlc ---SELECT * FROM items_init where seq = 1 ---SELECT * FROM items where seq = 1 -----join conversion back to data initial part link------------------------------ -,joinback_init AS ( -SELECT - i.stlc - ,i.coltier - ,i.branding - ,i.accs - ,i.suffix - ,i.pckg - ,i.col - ,i.vol_uom - ,i.vol_qty - ,i.price - ,c.nm - ,c.dm - ,l.clist -FROM - items_init i - LEFT OUTER JOIN conversion c ON - c.p = i.item - AND c.f = i.vol_uom - NATURAL JOIN colors l -WHERE - i.seq = 1 -ORDER BY - i.stlc - ,i.coltier - ,i.col -) -,agg_levels AS ( - SELECT - stlc - ,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier - ,string_agg(DISTINCT branding,', ') branding - ,string_agg(DISTINCT accs ,', ') accs - ,string_agg(DISTINCT suffix ,', ') suffix - ,pckg - ,col - ,vol_uom - ,voL_qty - ,price - ,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist - FROM - joinback_init - GROUP BY - stlc - ,pckg - ,col - ,vol_uom - ,vol_qty - ,price -) -SELECT - * -FROM - agg_levels +SELECT * FROM items_init WHERE stlc = 'TRV55420' +------compile all the conversions into a single json object---------------------- +--,items AS ( +-- SELECT +-- jsonb_agg( DISTINCT +-- jsonb_build_object( +-- 'partn', item +-- ,'fu', vol_uom +-- ,'tu','PC' +-- ) +-- ) conv +-- FROM +-- items_init +-- WHERE +-- seq = 1 +-- AND COALESCE(item,'') <> '' +--) +----do the conversion------------------------------------------------------------- +--, conversion AS ( +-- SELECT +-- rslt.* +-- FROM +-- items +-- JOIN LATERAL rlarp.uom_array(conv) rslt ON true +--) +------join conversion back to data initial part link------------------------------ +--,joinback_init AS ( +--SELECT +-- i.stlc +-- ,i.coltier +-- ,i.branding +-- ,i.accs +-- ,i.suffix +-- ,i.pckg +-- ,i.col +-- ,i.vol_uom +-- ,i.vol_qty +-- ,i.price +-- ,c.nm +-- ,c.dm +-- ,l.clist +--FROM +-- items_init i +-- LEFT OUTER JOIN conversion c ON +-- c.p = i.item +-- AND c.f = i.vol_uom +-- NATURAL JOIN colors l +--WHERE +-- i.seq = 1 +--ORDER BY +-- i.stlc +-- ,i.coltier +-- ,i.col +--) +------aggregate everything offered for the price point---------------------------- +--,agg_levels AS ( +-- SELECT +-- stlc +-- ,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier +-- ,string_agg(DISTINCT branding,', ') branding +-- -------replace blank with 'none'-------------- +-- ,regexp_replace(string_agg(DISTINCT accs ,', '),'^,','none,','g') accs +-- ,string_agg(DISTINCT suffix ,', ') suffix +-- ,pckg +-- ,col +-- ,vol_uom +-- ,voL_qty +-- ,nm +-- ,dm +-- ,price +-- -----convert json array to a plain text representation as well as remove nulls------ +-- ,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist +-- FROM +-- joinback_init +-- GROUP BY +-- stlc +-- ,pckg +-- ,col +-- ,vol_uom +-- ,vol_qty +-- ,nm +-- ,dm +-- ,price +--) +--,seq_levels AS ( +-- SELECT +-- stlc||'.'||pckg||'.'||col||'.'||to_char(row_number() OVER (PARTITION BY stlc, col ORDER BY price ASC),'FM00') lookup +-- ,stlc +-- ,coltier +-- ,branding +-- ,accs +-- ,suffix +-- ,pckg +-- ,clist +-- ,col +-- ,vol_uom +-- ,vol_qty +-- ,nm +-- ,dm +-- ,price +-- FROM +-- agg_levels +--) +--SELECT * FROM seq_levels