diff --git a/live_quote_mv.pg.sql b/live_quote_mv.pg.sql deleted file mode 100644 index 7eb7d43..0000000 --- a/live_quote_mv.pg.sql +++ /dev/null @@ -1,70 +0,0 @@ - -set work_mem TO '4GB'; - -DROP TABLE IF EXISTS rlarp.live_quotes_review_mv; -CREATE TABLE rlarp.live_quotes_review_mv AS -WITH -lq AS MATERIALIZED ( - SELECT - lq.* - ,substring(lq.part,1,8) mold - FROM - pricequote.live_quotes lq -) -,lqg AS ( - SELECT - lq.* - ,pricing->'product'->>'mold' part_group - ,substring(pricing->'customer'->>'chan',1,1) qchan - ,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 fstd_usd - ,(pricing->'guidance'->>'ltp')::numeric ltp - ,(pricing->'guidance'->>'optimization')::numeric optimization - ,(pricing->'guidance'->>'inflationFactor')::numeric inflation - ,jsonb_pretty(pricing) pricing - FROM - lq - LEFT JOIN LATERAL rlarp.guidance_r1( - rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) - ) pricing ON TRUE - WHERE - lq.qstat ~ 'Submitted' -) -,hist AS ( - SELECT - g.* - ,gset.chan - --,gset.mold moldh - ,gset.v1ds v1dsh - ,gset.cust - ,gset.vers - ,je.k - ,seas.* - FROM - lqg g - LEFT OUTER JOIN rlarp.price_pool_dev p ON - p.gset @> jsonb_build_object('mold',g.part_group) - AND p.gset ? 'cust' - AND p.gset ? 'v1ds' - LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( - chan text - ,mold text - ,v1ds text - ,v0ds text - ,cust text - ,vers text - --,nurs text - --,ghse text - ) ON TRUE - LEFT JOIN LATERAL jsonb_each(p.season) je(k,v) on true - LEFT JOIN Lateral jsonb_to_record(je.v) as seas( - units numeric - ,sales_usd numeric - ,price_usd numeric - ) ON TRUE -) -SELECT * FROM hist; diff --git a/sql/consume_pricepool.pg.sql b/sql/consume_pricepool.pg.sql index e61608d..a1e4775 100644 --- a/sql/consume_pricepool.pg.sql +++ b/sql/consume_pricepool.pg.sql @@ -10,7 +10,7 @@ SELECT ,je.k ,seas.* FROM - rlarp.price_pool_dev p + rlarp.price_pool p LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( chan text ,mold text diff --git a/sql/get_hist.pg.sql b/sql/get_hist.pg.sql index ecdb64e..487e416 100644 --- a/sql/get_hist.pg.sql +++ b/sql/get_hist.pg.sql @@ -18,7 +18,7 @@ sort AS ( ,stats.* ,season FROM - rlarp.price_pool_dev p + rlarp.price_pool p JOIN LATERAL jsonb_to_record(gset) AS gset( chan text ,mold text diff --git a/sql/get_hist_debug.pg.sql b/sql/get_hist_debug.pg.sql index 8a0517d..2784143 100644 --- a/sql/get_hist_debug.pg.sql +++ b/sql/get_hist_debug.pg.sql @@ -11,7 +11,7 @@ sel AS (select 'v1:T..PLT..' _v1ds, 'KAWAHARA NURSERY' _cust, 'AZE10001' _mold, ,stats.* ,season FROM - rlarp.price_pool_dev p + rlarp.price_pool p CROSS JOIN sel JOIN LATERAL jsonb_to_record(gset) AS gset( chan text diff --git a/sql/gethist.sql b/sql/gethist.sql index a90845f..a8d88af 100644 --- a/sql/gethist.sql +++ b/sql/gethist.sql @@ -25,7 +25,7 @@ BEGIN stats, (SELECT doc FROM getj) AS gdoc FROM - rlarp.price_pool_dev p + rlarp.price_pool p WHERE gset @> jsonb_build_object( 'mold', (SELECT doc->>'mold' FROM getj), diff --git a/sql/gethist_table.pg.sql b/sql/gethist_table.pg.sql index 965b61e..6dd876d 100644 --- a/sql/gethist_table.pg.sql +++ b/sql/gethist_table.pg.sql @@ -10,7 +10,7 @@ sel AS (select 'v1:P.P.PLT..' _v1ds, 'ALTMAN PLANTS' _cust, 'TFR001G0' _mold, 'D ,row_number() OVER (PARTITION BY p.agglevel ORDER BY avgunits DESC) rn ,stats.* FROM - rlarp.price_pool_dev p + rlarp.price_pool p CROSS JOIN sel JOIN LATERAL jsonb_to_record(gset) AS gset( chan text diff --git a/sql/live_quote_nohist.pg.sql b/sql/live_quote_nohist.pg.sql deleted file mode 100644 index 62e4f86..0000000 --- a/sql/live_quote_nohist.pg.sql +++ /dev/null @@ -1,69 +0,0 @@ -DROP TABLE IF EXISTS rlarp.live_quotes_nohist; - -CREATE TABLE rlarp.live_quotes_nohist AS -WITH -lq AS MATERIALIZED ( - SELECT - lq.* - ,substring(lq.part,1,8) mold - FROM - pricequote.live_quotes lq -) -,lqg AS ( - SELECT - lq.* - ,pricing->'product'->>'mold' part_group - ,substring(pricing->'customer'->>'chan',1,1) qchan - ,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 fstd_usd - ,(pricing->'guidance'->>'ltp')::numeric ltp - ,(pricing->'guidance'->>'optimization')::numeric optimization - ,(pricing->'guidance'->>'inflationFactor')::numeric inflation - ,jsonb_pretty(pricing) pricing - FROM - lq - LEFT JOIN LATERAL rlarp.guidance_r1( - rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) - ) pricing ON TRUE - WHERE - TRUE - --lq.qstat ~ 'Submitted' -) ---,hist AS ( --- SELECT --- g.* --- ,gset.chan --- --,gset.mold moldh --- ,gset.v1ds v1dsh --- ,gset.cust --- ,gset.vers --- ,je.k --- ,seas.* --- FROM --- lqg g --- LEFT OUTER JOIN rlarp.price_pool_dev p ON --- p.gset @> jsonb_build_object('mold',g.part_group) --- AND p.gset ? 'cust' --- AND p.gset ? 'v1ds' --- LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( --- chan text --- ,mold text --- ,v1ds text --- ,v0ds text --- ,cust text --- ,vers text --- --,nurs text --- --,ghse text --- ) ON TRUE --- LEFT JOIN LATERAL jsonb_each(p.season) je(k,v) on true --- LEFT JOIN Lateral jsonb_to_record(je.v) as seas( --- units numeric --- ,sales_usd numeric --- ,price_usd numeric --- ) ON TRUE ---) -SELECT * FROM lqg; diff --git a/sql/live_quotes_review.pg.sql b/sql/live_quotes_review.pg.sql index 11a9034..1db3eb8 100644 --- a/sql/live_quotes_review.pg.sql +++ b/sql/live_quotes_review.pg.sql @@ -46,7 +46,7 @@ lq AS MATERIALIZED ( ,seas.* FROM lqg g - LEFT OUTER JOIN rlarp.price_pool_dev p ON + LEFT OUTER JOIN rlarp.price_pool p ON p.gset @> jsonb_build_object('mold',g.part_group) AND p.gset ? 'cust' AND p.gset ? 'v1ds' diff --git a/sql/livequotes_dev.pg.sql b/sql/livequotes_dev.pg.sql deleted file mode 100644 index 54a4107..0000000 --- a/sql/livequotes_dev.pg.sql +++ /dev/null @@ -1,59 +0,0 @@ -WITH -lq AS MATERIALIZED ( - SELECT - lq.* - ,substring(lq.part,1,8) mold - FROM - pricequote.live_quotes lq -) -,lqg AS ( - SELECT - lq.* - ,m.part_group - ,(pricing->'guidance'->'finalPrice'->>'Price')::numeric guidance - ,pricing->'guidance'->'finalPrice'->>'Reason' reason - ,jsonb_pretty(pricing) pricing - FROM - lq - JOIN LATERAL rlarp.guidance_r1( - rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) - ) pricing ON TRUE - LEFT OUTER JOIN rlarp.molds m ON - m.stlc = lq.mold - WHERE - lq.qstat ~ 'Submitted' -) -,hist AS ( - SELECT - g.* - ,gset.chan - --,gset.mold moldh - ,gset.v1ds v1dsh - ,gset.cust - ,gset.vers - ,je.k - ,seas.* - FROM - lqg g - LEFT OUTER JOIN rlarp.price_pool_dev p ON - p.gset @> jsonb_build_object('mold',g.part_group) - AND p.gset ? 'cust' - AND p.gset ? 'v1ds' - LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( - chan text - ,mold text - ,v1ds text - ,v0ds text - ,cust text - ,vers text - --,nurs text - --,ghse text - ) ON TRUE - LEFT JOIN LATERAL jsonb_each(p.season) je(k,v) on true - LEFT JOIN Lateral jsonb_to_record(je.v) as seas( - units numeric - ,sales_usd numeric - ,price_usd numeric - ) ON TRUE -) -SELECT * FROM hist WHERE qid = 73059 diff --git a/sql/price_pool.sql b/sql/price_pool.sql index 7dea9f4..828d8da 100644 --- a/sql/price_pool.sql +++ b/sql/price_pool.sql @@ -2,9 +2,9 @@ --LANGUAGE plpgsql AS --$func$ --BEGIN; -DROP MATERIALIZED VIEW IF EXISTS rlarp.price_pool_dev CASCADE; ---CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS ( -CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( +-- DROP MATERIALIZED VIEW IF EXISTS rlarp.price_pool CASCADE; +--CREATE TABLE IF NOT EXISTS rlarp.price_pool AS ( +CREATE MATERIALIZED VIEW rlarp.price_pool AS ( WITH agg AS ( SELECT @@ -12,13 +12,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( ,substring(o.version,1,1) version ,o.chanwide ,o.nursery_region - ,c.greenhouse_region - ,m.part_group baseitem - ,m.majg - ,m.assc - ,i.coltier - ,'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) v1dataseg - ,_ds.dataseg v0dataseg + ,o.greenhouse_region + ,o.partgroup baseitem + ,o.majg + ,o.assc + ,o.coltier + ,o.dataseg v1dataseg + ,o.v0dataseg v0dataseg ,o.oseas oseas ,round(sum(o.qty),0) units ,round(sum(o.sales_usd),0) sales_usd @@ -26,35 +26,26 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( ,count(DISTINCT o.ordnum) ordcount FROM rlarp.osm_stack o - INNER JOIN "CMS.CUSLG".itemm i ON - i.item = o.part - LEFT OUTER JOIN rlarp.molds m ON - m.stlc = i.stlc - LEFT OUTER JOIN _ds ON - _ds.colgrp = o.colgrp - AND _ds.brand = substring(i.branding,1,1) LEFT OUTER JOIN pricequote.market_setavgprice tp ON tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) AND tp.country = 'ALL' AND tp.geo = 'ALL' AND tp.region = 'ALL' - AND tp.mold = i.stlc + AND tp.mold = o.stlc AND tp.chan = 'DISTRIB DROP SHIP' - AND tp.data_segment = 'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) + AND tp.data_segment = o.dataseg LEFT OUTER JOIN pricequote.market_setavgprice tq ON tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) AND tq.country = 'ALL' AND tq.geo = 'ALL' AND tq.region = 'ALL' - AND tq.mold = i.stlc + AND tq.mold = o.stlc AND tq.chan = 'DISTRIB DROP SHIP' - AND tq.data_segment = _ds.dataseg - LEFT OUTER JOIN rlarp.cust c ON - c.code = CASE o.chan WHEN 'DIR' THEN o.bill_cust ELSE o.ship_cust END + AND tq.data_segment = o.v0dataseg WHERE o.version IN ('Actual','Quotes') AND o.oseas >= 2015 - AND o.dsm <> 'PW' + AND o.ming <> 'B52' --AND o.part like 'SQL035%' --AND o.calc_status <> 'CANCELED' --AND o.fs_line = '41010' @@ -65,13 +56,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( ,substring(o.version,1,1) ,o.chanwide ,o.nursery_region - ,c.greenhouse_region - ,m.part_group - ,m.majg - ,m.assc - ,i.coltier - ,'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) - ,_ds.dataseg + ,o.greenhouse_region + ,o.partgroup + ,o.majg + ,o.assc + ,o.coltier + ,o.dataseg + ,o.v0dataseg ,o.oseas HAVING round(sum(o.qty),0) > 0 @@ -82,49 +73,40 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( ,substring(o.version,1,1) version ,o.chanwide ,o.nursery_region - ,c.greenhouse_region - ,m.part_group baseitem - ,m.majg - ,m.assc - ,i.coltier - ,'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) v1dataseg - ,_ds.dataseg v0dataseg - ,0 oseas + ,o.greenhouse_region + ,o.partgroup baseitem + ,o.majg + ,o.assc + ,o.coltier + ,o.dataseg v1dataseg + ,o.v0dataseg v0dataseg + ,o.oseas oseas ,round(sum(o.qty),0) units ,round(sum(o.sales_usd),0) sales_usd ,round(sum(COALESCE(tp.target_price,tq.target_price) * o.qty),2) target_price ,count(DISTINCT o.ordnum) ordcount FROM rlarp.osm_stack o - INNER JOIN "CMS.CUSLG".itemm i ON - i.item = o.part - LEFT OUTER JOIN rlarp.molds m ON - m.stlc = i.stlc - LEFT OUTER JOIN _ds ON - _ds.colgrp = o.colgrp - AND _ds.brand = substring(i.branding,1,1) LEFT OUTER JOIN pricequote.market_setavgprice tp ON tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) AND tp.country = 'ALL' AND tp.geo = 'ALL' AND tp.region = 'ALL' - AND tp.mold = i.stlc + AND tp.mold = o.stlc AND tp.chan = 'DISTRIB DROP SHIP' - AND tp.data_segment = 'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) + AND tp.data_segment = o.dataseg LEFT OUTER JOIN pricequote.market_setavgprice tq ON tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) AND tq.country = 'ALL' AND tq.geo = 'ALL' AND tq.region = 'ALL' - AND tq.mold = i.stlc + AND tq.mold = o.stlc AND tq.chan = 'DISTRIB DROP SHIP' - AND tq.data_segment = _ds.dataseg - LEFT OUTER JOIN rlarp.cust c ON - c.code = CASE o.chan WHEN 'DIR' THEN o.bill_cust ELSE o.ship_cust END + AND tq.data_segment = o.v0dataseg WHERE o.version IN ('Actual','Quotes') AND o.odate >= current_date - '4 months'::interval - AND o.dsm <> 'PW' + AND o.ming <> 'B52' --AND o.part like 'SQL035%' --AND o.calc_status <> 'CANCELED' --AND o.fs_line = '41010' @@ -135,13 +117,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( ,substring(o.version,1,1) ,o.chanwide ,o.nursery_region - ,c.greenhouse_region - ,m.part_group - ,m.majg - ,m.assc - ,i.coltier - ,'v1:' || rtrim(COALESCE(i.coltier, ''))|| '.' || rtrim(substring(COALESCE(i.branding, ''), 1, 1))|| '.' || rtrim(COALESCE(i.uomp, ''))|| '.' || rtrim(COALESCE(i.suffix, ''))|| '.' || rtrim(COALESCE(i.accs_ps, '')) - ,_ds.dataseg + ,o.greenhouse_region + ,o.partgroup + ,o.majg + ,o.assc + ,o.coltier + ,o.dataseg + ,o.v0dataseg ,o.oseas HAVING round(sum(o.qty),0) > 0 @@ -241,5 +223,5 @@ CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( --LIMIT 1000 ) WITH DATA; -create index ppd_gset on rlarp.price_pool_dev using gin (gset); +create index ppd_gset on rlarp.price_pool using gin (gset); --END; diff --git a/sql/quote_review.pg.sql b/sql/quote_review.pg.sql index 976dcad..be7d0b9 100644 --- a/sql/quote_review.pg.sql +++ b/sql/quote_review.pg.sql @@ -115,7 +115,7 @@ lq AS MATERIALIZED ( ,seas.* FROM lqg g - LEFT OUTER JOIN rlarp.price_pool_dev p ON + LEFT OUTER JOIN rlarp.price_pool p ON p.gset @> jsonb_build_object('mold',g.part_group) AND p.gset ? 'cust' AND p.gset ? 'v1ds'