diff --git a/sql/quote_review.pg.sql b/sql/quote_review.pg.sql index be7d0b9..1fac56b 100644 --- a/sql/quote_review.pg.sql +++ b/sql/quote_review.pg.sql @@ -3,6 +3,7 @@ DROP VIEW IF EXISTS rlarp.quote_review; CREATE VIEW rlarp.quote_review AS WITH +---------------------get quote lines from SQL Server--------------------- lq AS MATERIALIZED ( SELECT lq.* @@ -12,6 +13,7 @@ lq AS MATERIALIZED ( WHERE qstat LIKE 'Submitted%' ) +---------------------guidance logic--------------------------------------- ,lqg AS ( SELECT lq.qid @@ -92,6 +94,9 @@ lq AS MATERIALIZED ( END END END END END guidance + ,pl.price pricelist + ,pl.listcode + ,row_number() OVER (PARTITION BY qid, qline ORDER BY pl.price ASC) bestprice -- ,(pricing->'guidance'->>'ltp')::numeric ltp -- ,(pricing->'guidance'->>'optimization')::numeric optimization -- ,(pricing->'guidance'->>'inflationFactor')::numeric inflation @@ -100,9 +105,23 @@ lq AS MATERIALIZED ( lq LEFT OUTER JOIN "CMS.CUSLG".itemm i ON i.item = lq.part + LEFT OUTER JOIN rlarp.cust bc ON + bc.code = lq.billto + LEFT OUTER JOIN rlarp.cust sc ON + sc.code = lq.shipto + LEFT OUTER JOIN rlarp.plcore_fullcode_ranged pl ON + pl.item = lq.part + AND jsonb_build_array(pl.listcode) <@ (COALESCE(sc.lists,bc.lists)) + AND ( + lq.units_each >= pl.vb_f + AND lq.units_each < pl.vb_t + ) WHERE lq.qstat ~ 'Submitted' ) +-- SELECT * FROM lqg where bestprice <> 1 +-- SELECT * FROM lqg WHERE qid = 109352 +---------------------------link in sales history---------------------------------------------- ,hist AS ( SELECT g.* @@ -135,5 +154,7 @@ lq AS MATERIALIZED ( ,sales_usd numeric ,price_usd numeric ) ON TRUE + WHERE + COALESCE(g.bestprice,1) = 1 ) SELECT * FROM hist --WHERE qid = 108655