This commit is contained in:
Paul Trowbridge 2021-01-08 17:59:05 -05:00
parent 397b309a55
commit 85e288cf11

View File

@ -81,37 +81,96 @@ GROUP BY
) )
-----------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-------------------------
,items_init AS ( ,unpivot AS (
SELECT SELECT
pc.STLC pc.stlc
,pc.coltier ,pc.coltier
,pc.branding ,pc.branding
,pc.accs ,pc.accs
,pc.suffix ,pc.suffix
,row_number() OVER (PARTITION BY pc.stlc, pc.coltier, pc.branding, pc.accs, pc.suffix ORDER BY item DESC) seq ,pc.pckg1 pckg
,item ,pc.vol1_uom vol_uom
,vol1_uom
,vol2_uom
,vol3_uom
,'PC' to_uom
FROM FROM
pc pc
UNION
SELECT
pc.stlc
,pc.coltier
,pc.branding
,pc.accs
,pc.suffix
,pc.pckg2 pckg
,pc.vol2_uom vol_uom
FROM
pc
UNION
SELECT
pc.stlc
,pc.coltier
,pc.branding
,pc.accs
,pc.suffix
,pc.pckg3 pckg
,pc.vol3_uom vol_uom
FROM
pc
)
----link to actual part numbers, tag the sequence so a single item can be chosen-----
,items_init AS (
SELECT
up.STLC
,up.coltier
,up.branding
,up.accs
,up.suffix
,up.pckg
,up.vol_uom
,row_number() OVER (PARTITION BY up.stlc, up.coltier, up.branding, up.accs, up.suffix ORDER BY item DESC) seq
,item
FROM
unpivot up
LEFT OUTER JOIN RLARP.ITEMMv i ON LEFT OUTER JOIN RLARP.ITEMMv i ON
i.STLC = pc.stlc i.STLC = up.stlc
--AND i.COLTIER = pc.coltier --AND i.COLTIER = pc.coltier
AND COALESCE(i.BRANDING,'') = pc.branding AND i.uomp ? up.pckg
AND COALESCE(i.ACCS,'') = pc.ACCS AND COALESCE(i.BRANDING,'') = up.branding
AND COALESCE(i.SUFFIX,'') = pc.suffix AND COALESCE(i.ACCS,'') = up.ACCS
AND COALESCE(i.SUFFIX,'') = up.suffix
AND APLNT <> 'I' AND APLNT <> 'I'
) )
----compile all the conversions into a single json object
,items AS ( ,items AS (
SELECT * FROM items_init where seq = 1 SELECT
jsonb_agg( DISTINCT
jsonb_build_object(
'partn', item
,'fu', vol_uom
,'tu','PC'
) )
,uom AS ( ) conv
SELECT item partn, vol1_uom from_uom, 'pc' to_uom FROM items_init FROM
UNION ALL items_init
SELECT item partn, vol2_uom from_uom, 'pc' to_uom FROM items_init WHERE
UNION ALL seq = 1
SELECT item partn, vol3_uom from_uom, 'pc' to_uom FROM items_init AND COALESCE(item,'') <> ''
) )
SELECT * FROM uom , conversion AS (
SELECT
rslt.*
FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
--SELECT * FROM conversion
SELECT
i.*
,c.*
FROM
items_init i
LEFT OUTER JOIN conversion c ON
c.p = i.item
WHERE
i.seq = 1
ORDER BY
stlc
,coltier