diff --git a/sql/quote_review.pg.sql b/sql/quote_review.pg.sql index 7507034..86fe3b6 100644 --- a/sql/quote_review.pg.sql +++ b/sql/quote_review.pg.sql @@ -1,5 +1,5 @@ -set work_mem TO '4GB'; - +-- set work_mem TO '4GB'; +-- DROP VIEW IF EXISTS rlarp.quote_review; CREATE VIEW rlarp.quote_review AS WITH @@ -12,41 +12,57 @@ lq AS MATERIALIZED ( ) ,lqg AS ( SELECT - lq.* - ,pricing->'product'->>'mold' part_group - CASE SUBSTR(bc.cclass,2,3) - --if the bill to class is ditsributor, then it's either warehouse or drop - WHEN 'DIS' THEN - --if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse - CASE SUBSTR(sc.cclass,2,3) - WHEN 'DIS' THEN 'WHS' - ELSE 'DRP' - END - --CASE WHEN RTRIM(SUBSTR(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTR(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END - --everything else does not involve a distributor and is considered direct - ELSE 'DIR' - END qchan, - ,CASE osm.chan WHEN 'DRP' THEN rtrim(osm.shipgrp) ELSE rtrim(osm.account) END customer - ,pricing->'customer'->>'cust' qcust - ,pricing->'product'->>'itemrel' item_fit - ,(pricing->'product'->>'pltq')::numeric pltq - ,(pricing->'guidance'->'finalPrice'->>'Price')::numeric guidance - ,pricing->'guidance'->'finalPrice'->>'Reason' reason - ,(pricing->'product'->>'cstd_usd_ina')::numeric cstd_usd - ,(pricing->'product'->>'fstd_usd_ina')::numeric fstd_usd - ,(pricing->'guidance'->>'ltp')::numeric ltp - ,(pricing->'guidance'->>'optimization')::numeric optimization - ,(pricing->'guidance'->>'inflationFactor')::numeric inflation - ,jsonb_pretty(pricing) pricing + lq.qid + ,lq.qline + ,lq.rep + ,lq.touched + ,lq.expires + ,lq.request + ,lq.qtitle + ,lq.qstatid + ,lq.qstat + ,lq.quotenumber + ,lq.billto + ,lq.shipto + ,lq.qchan + ,lq.qcustomer + ,lq.part + ,lq.qoptions + ,lq.partbuilt + ,lq.colgrp + ,lq.colc + ,lq.coltier + ,lq.brand + ,lq.dataseg + ,lq.v1ds + ,lq.comment + ,lq.units_each + ,lq.price + ,lq.sales + ,lq.histprice + ,lq.targetp + ,lq.lastsalesprice + ,lq.r_curr + ,lq.qt_rate + ,lq.qrn + ,lq.url + ,lq.tacticalmodifier + ,lq.finalrecommendedprice + ,i.partgroup part_group + -- ,pricing->'product'->>'itemrel' item_fit + ,i.mpck pltq + -- ,(pricing->'guidance'->'finalPrice'->>'Price')::numeric guidance + -- ,pricing->'guidance'->'finalPrice'->>'Reason' reason + ,i.curstdus cstd_usd + ,i.futstdus fstd_usd + -- ,(pricing->'guidance'->>'ltp')::numeric ltp + -- ,(pricing->'guidance'->>'optimization')::numeric optimization + -- ,(pricing->'guidance'->>'inflationFactor')::numeric inflation + -- ,jsonb_pretty(pricing) pricing FROM lq - LEFT OUTER JOIN rlarp.cust bc ON - bc.cust = lq.billto - LEFT OUTER JOIN rlarp.cust sc ON - bc.cust = lq.shipto - LEFT JOIN LATERAL rlarp.guidance_r1( - rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2025) - ) pricing ON TRUE + LEFT OUTER JOIN "CMS.CUSLG".itemm i ON + i.item = lq.part WHERE lq.qstat ~ 'Submitted' )