Compare commits
No commits in common. "e040132bce2d9761f9d636fc3e3c0feffd3c4a07" and "460912af7c39cc0a933538948da61b46ea50f833" have entirely different histories.
e040132bce
...
460912af7c
@ -1,53 +1,54 @@
|
|||||||
--EXPLAIN (ANALYZE, BUFFERS)
|
--EXPLAIN (ANALYZE, BUFFERS)
|
||||||
WITH
|
WITH
|
||||||
-------------make BDL and SLV the same thing-------------------------------------------------------
|
pc AS materialized (
|
||||||
-------------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
|
SELECT
|
||||||
plist
|
STLC
|
||||||
,STLC
|
|
||||||
,COALESCE(COLTIER,'') COLTIER
|
,COALESCE(COLTIER,'') COLTIER
|
||||||
,coalesce(BRANDING,'' ) branding
|
,coalesce(BRANDING,'' ) branding
|
||||||
,COALESCE(ACCS ,'') ACCS
|
,COALESCE(ACCS ,'') ACCS
|
||||||
,COALESCE(SUFF,'') SUFFIX
|
,COALESCE(SUFF,'') SUFFIX
|
||||||
----price 1 -----------------
|
----price 1 -----------------
|
||||||
,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG1
|
,PCKG PCKG1
|
||||||
,1 VOLL1
|
,1 VOLL1
|
||||||
,PCKG VOL1_UOM
|
,PCKG VOL1_UOM
|
||||||
,PACK VOL1_PRICE
|
,PACK VOL1_PRICE
|
||||||
----price 2 -----------------
|
----price 2 -----------------
|
||||||
,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG2
|
,PCKG PCKG2
|
||||||
,1 VOLl2
|
,1 VOLl2
|
||||||
,'PLT' VOL2_UOM
|
,'PLT' VOL2_UOM
|
||||||
,MP VOL2_PRICE
|
,MP VOL2_PRICE
|
||||||
----price 3 -----------------
|
----price 3 -----------------
|
||||||
,jsonb_build_array('PLT') PCKG3
|
,'PLT' PCKG3
|
||||||
,1 VOLL3
|
,1 VOLL3
|
||||||
,'PLT' VOL3_UOM
|
,'PLT' VOL3_UOM
|
||||||
,BULK VOL3_PRICE
|
,BULK VOL3_PRICE
|
||||||
FROM
|
FROM
|
||||||
rlarp.PCORE P
|
rlarp.PCORE P
|
||||||
)
|
)
|
||||||
--------------list unique colors per line-----------------------------------------------------------
|
|
||||||
,colors AS (
|
,colors AS (
|
||||||
SELECT
|
SELECT
|
||||||
pc.plist
|
pc.STLC
|
||||||
,pc.STLC
|
|
||||||
,pc.COLTIER
|
,pc.COLTIER
|
||||||
,pc.branding
|
,pc.branding
|
||||||
,pc.ACCS
|
,pc.ACCS
|
||||||
,pc.SUFFIX
|
,pc.SUFFIX
|
||||||
,string_agg( DISTINCT ltrim(rtrim(i.COLC)),', ') tclist
|
,string_agg( DISTINCT ltrim(rtrim(i.COLC)),', ') tclist
|
||||||
,jsonb_agg( DISTINCT ltrim(rtrim(i.COLC))) clist
|
,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
|
FROM
|
||||||
pc
|
pc
|
||||||
LEFT OUTER JOIN RLARP.ITEMMv i ON
|
LEFT OUTER JOIN RLARP.ITEMMv i ON
|
||||||
@ -59,19 +60,32 @@ FROM
|
|||||||
--AND COALESCE(i.SUFFIX,'') = pc.suffix
|
--AND COALESCE(i.SUFFIX,'') = pc.suffix
|
||||||
AND APLNT <> 'I'
|
AND APLNT <> 'I'
|
||||||
GROUP BY
|
GROUP BY
|
||||||
pc.plist
|
pc.STLC
|
||||||
,pc.STLC
|
|
||||||
,pc.COLTIER
|
,pc.COLTIER
|
||||||
,pc.branding
|
,pc.branding
|
||||||
,pc.ACCS
|
,pc.ACCS
|
||||||
,pc.SUFFIX
|
,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---------------
|
-----------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-------------------------
|
-----------must first pivot the data out then join based on the type of row-------------------------
|
||||||
,unpivot AS (
|
,unpivot AS (
|
||||||
SELECT
|
SELECT
|
||||||
pc.plist
|
pc.stlc
|
||||||
,pc.stlc
|
|
||||||
,pc.coltier
|
,pc.coltier
|
||||||
,pc.branding
|
,pc.branding
|
||||||
,pc.accs
|
,pc.accs
|
||||||
@ -83,12 +97,9 @@ GROUP BY
|
|||||||
,pc.vol1_price price
|
,pc.vol1_price price
|
||||||
FROM
|
FROM
|
||||||
pc
|
pc
|
||||||
WHERE
|
|
||||||
pc.vol1_price IS NOT null
|
|
||||||
UNION
|
UNION
|
||||||
SELECT
|
SELECT
|
||||||
pc.plist
|
pc.stlc
|
||||||
,pc.stlc
|
|
||||||
,pc.coltier
|
,pc.coltier
|
||||||
,pc.branding
|
,pc.branding
|
||||||
,pc.accs
|
,pc.accs
|
||||||
@ -100,12 +111,9 @@ GROUP BY
|
|||||||
,pc.vol2_price price
|
,pc.vol2_price price
|
||||||
FROM
|
FROM
|
||||||
pc
|
pc
|
||||||
WHERE
|
|
||||||
pc.vol2_price IS NOT null
|
|
||||||
UNION
|
UNION
|
||||||
SELECT
|
SELECT
|
||||||
pc.plist
|
pc.stlc
|
||||||
,pc.stlc
|
|
||||||
,pc.coltier
|
,pc.coltier
|
||||||
,pc.branding
|
,pc.branding
|
||||||
,pc.accs
|
,pc.accs
|
||||||
@ -117,16 +125,11 @@ GROUP BY
|
|||||||
,pc.vol3_price price
|
,pc.vol3_price price
|
||||||
FROM
|
FROM
|
||||||
pc
|
pc
|
||||||
WHERE
|
|
||||||
pc.vol3_price IS NOT null
|
|
||||||
)
|
)
|
||||||
--SELECT * FROM unpivot WHERE pckg ? 'SLV'
|
----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 (
|
,items_init AS (
|
||||||
SELECT
|
SELECT
|
||||||
up.plist
|
up.STLC
|
||||||
,up.stlc
|
|
||||||
,up.coltier
|
,up.coltier
|
||||||
,up.branding
|
,up.branding
|
||||||
,up.accs
|
,up.accs
|
||||||
@ -137,27 +140,34 @@ SELECT
|
|||||||
,up.vol_uom
|
,up.vol_uom
|
||||||
,up.voL_qty
|
,up.voL_qty
|
||||||
,up.price
|
,up.price
|
||||||
,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
|
,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
|
||||||
,item
|
,item
|
||||||
--subquery option is not performant at all
|
--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
|
FROM
|
||||||
unpivot up
|
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
|
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
|
||||||
--basically the same as does the core list package match the item master package
|
AND i.uomp ? up.pckg
|
||||||
--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' 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
|
||||||
@ -182,16 +192,19 @@ FROM
|
|||||||
items
|
items
|
||||||
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
|
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------------------------------
|
----join conversion back to data initial part link------------------------------
|
||||||
,joinback_init AS (
|
,joinback_init AS (
|
||||||
SELECT
|
SELECT
|
||||||
i.plist
|
i.stlc
|
||||||
,i.stlc
|
|
||||||
,i.coltier
|
,i.coltier
|
||||||
,i.branding
|
,i.branding
|
||||||
,i.accs
|
,i.accs
|
||||||
,i.suffix
|
,i.suffix
|
||||||
,i.pckg->>0 pckg
|
,i.pckg
|
||||||
,i.col
|
,i.col
|
||||||
,i.vol_uom
|
,i.vol_uom
|
||||||
,i.vol_qty
|
,i.vol_qty
|
||||||
@ -207,57 +220,35 @@ FROM
|
|||||||
NATURAL JOIN colors l
|
NATURAL JOIN colors l
|
||||||
WHERE
|
WHERE
|
||||||
i.seq = 1
|
i.seq = 1
|
||||||
|
ORDER BY
|
||||||
|
i.stlc
|
||||||
|
,i.coltier
|
||||||
|
,i.col
|
||||||
)
|
)
|
||||||
----aggregate everything offered for the price point----------------------------
|
|
||||||
,agg_levels AS (
|
,agg_levels AS (
|
||||||
SELECT
|
SELECT
|
||||||
plist
|
stlc
|
||||||
,stlc
|
|
||||||
,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier
|
,string_agg(DISTINCT coltier ,', ') FILTER (WHERE clist IS NOT NULL) coltier
|
||||||
,string_agg(DISTINCT branding,', ') branding
|
,string_agg(DISTINCT branding,', ') branding
|
||||||
-------replace blank with 'none'--------------
|
,string_agg(DISTINCT accs ,', ') accs
|
||||||
,regexp_replace(string_agg(DISTINCT accs ,', '),'^,','none,','g') accs
|
|
||||||
,string_agg(DISTINCT suffix ,', ') suffix
|
,string_agg(DISTINCT suffix ,', ') suffix
|
||||||
,pckg
|
,pckg
|
||||||
,col
|
,col
|
||||||
,vol_uom
|
,vol_uom
|
||||||
,voL_qty
|
,voL_qty
|
||||||
,nm
|
|
||||||
,dm
|
|
||||||
,price
|
,price
|
||||||
-----convert json array to a plain text representation as well as remove nulls------
|
|
||||||
,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist
|
,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist
|
||||||
FROM
|
FROM
|
||||||
joinback_init
|
joinback_init
|
||||||
GROUP BY
|
GROUP BY
|
||||||
plist
|
stlc
|
||||||
,stlc
|
|
||||||
,pckg
|
,pckg
|
||||||
,col
|
,col
|
||||||
,vol_uom
|
,vol_uom
|
||||||
,vol_qty
|
,vol_qty
|
||||||
,nm
|
|
||||||
,dm
|
|
||||||
,price
|
,price
|
||||||
)
|
)
|
||||||
,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
|
FROM
|
||||||
,plist
|
agg_levels
|
||||||
,stlc
|
|
||||||
,coltier
|
|
||||||
,branding
|
|
||||||
,accs
|
|
||||||
,suffix
|
|
||||||
,pckg
|
|
||||||
,clist
|
|
||||||
,col
|
|
||||||
,vol_uom
|
|
||||||
,vol_qty
|
|
||||||
,nm
|
|
||||||
,dm
|
|
||||||
,price
|
|
||||||
FROM
|
|
||||||
agg_levels
|
|
||||||
)
|
|
||||||
SELECT * FROM seq_levels
|
|
||||||
|
Loading…
Reference in New Issue
Block a user