diff --git a/db_schema/extract_for_customer_facing.pg.sql b/db_schema/extract_for_customer_facing.pg.sql index 97b40ab..861d4f3 100644 --- a/db_schema/extract_for_customer_facing.pg.sql +++ b/db_schema/extract_for_customer_facing.pg.sql @@ -81,37 +81,96 @@ GROUP BY ) -----------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 + ,pc.coltier + ,pc.branding + ,pc.accs + ,pc.suffix + ,pc.pckg1 pckg + ,pc.vol1_uom vol_uom + FROM + 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 - pc.STLC - ,pc.coltier - ,pc.branding - ,pc.accs - ,pc.suffix - ,row_number() OVER (PARTITION BY pc.stlc, pc.coltier, pc.branding, pc.accs, pc.suffix ORDER BY item DESC) seq + 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 - ,vol1_uom - ,vol2_uom - ,vol3_uom - ,'PC' to_uom FROM - pc + unpivot up LEFT OUTER JOIN RLARP.ITEMMv i ON - i.STLC = pc.stlc + i.STLC = up.stlc --AND i.COLTIER = pc.coltier - AND COALESCE(i.BRANDING,'') = pc.branding - AND COALESCE(i.ACCS,'') = pc.ACCS - AND COALESCE(i.SUFFIX,'') = pc.suffix + AND i.uomp ? up.pckg + 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 * FROM items_init where seq = 1 + SELECT + jsonb_agg( DISTINCT + jsonb_build_object( + 'partn', item + ,'fu', vol_uom + ,'tu','PC' + ) + ) conv + FROM + items_init + WHERE + seq = 1 + AND COALESCE(item,'') <> '' ) -,uom AS ( - SELECT item partn, vol1_uom from_uom, 'pc' to_uom FROM items_init - UNION ALL - SELECT item partn, vol2_uom from_uom, 'pc' to_uom FROM items_init - UNION ALL - SELECT item partn, vol3_uom from_uom, 'pc' to_uom FROM items_init -) -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