join to sleeves or bundles

This commit is contained in:
Paul Trowbridge 2021-01-12 15:57:00 -05:00
parent fee40dcb87
commit e040132bce

View File

@ -20,17 +20,17 @@ vu AS (
,COALESCE(ACCS ,'') ACCS ,COALESCE(ACCS ,'') ACCS
,COALESCE(SUFF,'') SUFFIX ,COALESCE(SUFF,'') SUFFIX
----price 1 ----------------- ----price 1 -----------------
,PCKG PCKG1 ,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG1
,1 VOLL1 ,1 VOLL1
,PCKG VOL1_UOM ,PCKG VOL1_UOM
,PACK VOL1_PRICE ,PACK VOL1_PRICE
----price 2 ----------------- ----price 2 -----------------
,PCKG PCKG2 ,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG2
,1 VOLl2 ,1 VOLl2
,'PLT' VOL2_UOM ,'PLT' VOL2_UOM
,MP VOL2_PRICE ,MP VOL2_PRICE
----price 3 ----------------- ----price 3 -----------------
,'PLT' PCKG3 ,jsonb_build_array('PLT') PCKG3
,1 VOLL3 ,1 VOLL3
,'PLT' VOL3_UOM ,'PLT' VOL3_UOM
,BULK VOL3_PRICE ,BULK VOL3_PRICE
@ -120,6 +120,7 @@ GROUP BY
WHERE WHERE
pc.vol3_price IS NOT null pc.vol3_price IS NOT null
) )
--SELECT * FROM unpivot WHERE pckg ? 'SLV'
--SELECT DISTINCT pckg FROM unpivot; --SELECT DISTINCT pckg FROM unpivot;
--link to actual part numbers, tag the sequence so a single item can be chosen----- --link to actual part numbers, tag the sequence so a single item can be chosen-----
,items_init AS ( ,items_init AS (
@ -143,118 +144,120 @@ FROM
unpivot up unpivot up
----make all the BDL units of measure BDL and also SLV with intentional duplication ----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 ----becusae some defaults units on itemm have SLV and not BDL, but still need a join
LEFT OUTER JOIN vu ON --LEFT OUTER JOIN vu ON
vu.bdl = up.pckg -- vu.bdl = up.pckg
LEFT OUTER JOIN RLARP.ITEMMv i ON LEFT OUTER JOIN RLARP.ITEMMv i ON
i.STLC = up.stlc i.STLC = up.stlc
--AND i.COLTIER = pc.coltier --AND i.COLTIER = pc.coltier
AND i.uomp ? vu.uom --basically the same as does the core list package match the item master package
--the item master could have multiple since its defined per plant but will take the first item for now
AND up.pckg ? (i.uomp->>0)
AND COALESCE(i.BRANDING,'') = up.branding AND COALESCE(i.BRANDING,'') = up.branding
AND COALESCE(i.ACCS,'') = up.ACCS AND COALESCE(i.ACCS,'') = up.ACCS
AND COALESCE(i.SUFFIX,'') = up.suffix AND COALESCE(i.SUFFIX,'') = up.suffix
AND APLNT <> 'I' AND APLNT <> 'I'
) )
SELECT * FROM items_init WHERE stlc = 'TRV55420' --SELECT * FROM items_init WHERE stlc = 'TRV55420' ORDER BY plist, stlc, col, seq
------compile all the conversions into a single json object---------------------- ----compile all the conversions into a single json object----------------------
--,items AS ( ,items AS (
-- SELECT SELECT
-- jsonb_agg( DISTINCT jsonb_agg( DISTINCT
-- jsonb_build_object( jsonb_build_object(
-- 'partn', item 'partn', item
-- ,'fu', vol_uom ,'fu', vol_uom
-- ,'tu','PC' ,'tu','PC'
-- ) )
-- ) conv ) conv
-- FROM FROM
-- items_init items_init
-- WHERE WHERE
-- seq = 1 seq = 1
-- AND COALESCE(item,'') <> '' AND COALESCE(item,'') <> ''
--) )
----do the conversion------------------------------------------------------------- --do the conversion-------------------------------------------------------------
--, conversion AS ( , conversion AS (
-- SELECT SELECT
-- rslt.* rslt.*
-- FROM FROM
-- items items
-- JOIN LATERAL rlarp.uom_array(conv) rslt ON true JOIN LATERAL rlarp.uom_array(conv) rslt ON true
--) )
------join conversion back to data initial part link------------------------------ ----join conversion back to data initial part link------------------------------
--,joinback_init AS ( ,joinback_init AS (
--SELECT SELECT
-- i.stlc i.plist
-- ,i.coltier ,i.stlc
-- ,i.branding ,i.coltier
-- ,i.accs ,i.branding
-- ,i.suffix ,i.accs
-- ,i.pckg ,i.suffix
-- ,i.col ,i.pckg->>0 pckg
-- ,i.vol_uom ,i.col
-- ,i.vol_qty ,i.vol_uom
-- ,i.price ,i.vol_qty
-- ,c.nm ,i.price
-- ,c.dm ,c.nm
-- ,l.clist ,c.dm
--FROM ,l.clist
-- items_init i FROM
-- LEFT OUTER JOIN conversion c ON items_init i
-- c.p = i.item LEFT OUTER JOIN conversion c ON
-- AND c.f = i.vol_uom c.p = i.item
-- NATURAL JOIN colors l AND c.f = i.vol_uom
--WHERE NATURAL JOIN colors l
-- i.seq = 1 WHERE
--ORDER BY i.seq = 1
-- i.stlc )
-- ,i.coltier ----aggregate everything offered for the price point----------------------------
-- ,i.col ,agg_levels AS (
--) SELECT
------aggregate everything offered for the price point---------------------------- plist
--,agg_levels AS ( ,stlc
-- SELECT ,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier
-- stlc ,string_agg(DISTINCT branding,', ') branding
-- ,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier -------replace blank with 'none'--------------
-- ,string_agg(DISTINCT branding,', ') branding ,regexp_replace(string_agg(DISTINCT accs ,', '),'^,','none,','g') accs
-- -------replace blank with 'none'-------------- ,string_agg(DISTINCT suffix ,', ') suffix
-- ,regexp_replace(string_agg(DISTINCT accs ,', '),'^,','none,','g') accs ,pckg
-- ,string_agg(DISTINCT suffix ,', ') suffix ,col
-- ,pckg ,vol_uom
-- ,col ,voL_qty
-- ,vol_uom ,nm
-- ,voL_qty ,dm
-- ,nm ,price
-- ,dm -----convert json array to a plain text representation as well as remove nulls------
-- ,price ,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist
-- -----convert json array to a plain text representation as well as remove nulls------ FROM
-- ,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist joinback_init
-- FROM GROUP BY
-- joinback_init plist
-- GROUP BY ,stlc
-- stlc ,pckg
-- ,pckg ,col
-- ,col ,vol_uom
-- ,vol_uom ,vol_qty
-- ,vol_qty ,nm
-- ,nm ,dm
-- ,dm ,price
-- ,price )
--) ,seq_levels AS (
--,seq_levels AS ( SELECT
-- SELECT plist||'.'||stlc||'.'||pckg||'.'||col||'.'||to_char(row_number() OVER (PARTITION BY plist, stlc, col ORDER BY price ASC),'FM00') lookup
-- stlc||'.'||pckg||'.'||col||'.'||to_char(row_number() OVER (PARTITION BY stlc, col ORDER BY price ASC),'FM00') lookup ,plist
-- ,stlc ,stlc
-- ,coltier ,coltier
-- ,branding ,branding
-- ,accs ,accs
-- ,suffix ,suffix
-- ,pckg ,pckg
-- ,clist ,clist
-- ,col ,col
-- ,vol_uom ,vol_uom
-- ,vol_qty ,vol_qty
-- ,nm ,nm
-- ,dm ,dm
-- ,price ,price
-- FROM FROM
-- agg_levels agg_levels
--) )
--SELECT * FROM seq_levels SELECT * FROM seq_levels