Compare commits

..

1 Commits

Author SHA1 Message Date
d0cbd464cc change name to not _dev 2025-01-15 02:51:26 -05:00
10 changed files with 263 additions and 47 deletions

70
live_quote_mv.pg.sql Normal file
View File

@ -0,0 +1,70 @@
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;

View File

@ -10,7 +10,7 @@ SELECT
,je.k ,je.k
,seas.* ,seas.*
FROM FROM
rlarp.price_pool p rlarp.price_pool_dev p
LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset(
chan text chan text
,mold text ,mold text

View File

@ -18,7 +18,7 @@ sort AS (
,stats.* ,stats.*
,season ,season
FROM FROM
rlarp.price_pool p rlarp.price_pool_dev p
JOIN LATERAL jsonb_to_record(gset) AS gset( JOIN LATERAL jsonb_to_record(gset) AS gset(
chan text chan text
,mold text ,mold text

View File

@ -11,7 +11,7 @@ sel AS (select 'v1:T..PLT..' _v1ds, 'KAWAHARA NURSERY' _cust, 'AZE10001' _mold,
,stats.* ,stats.*
,season ,season
FROM FROM
rlarp.price_pool p rlarp.price_pool_dev p
CROSS JOIN sel CROSS JOIN sel
JOIN LATERAL jsonb_to_record(gset) AS gset( JOIN LATERAL jsonb_to_record(gset) AS gset(
chan text chan text

View File

@ -25,7 +25,7 @@ BEGIN
stats, stats,
(SELECT doc FROM getj) AS gdoc (SELECT doc FROM getj) AS gdoc
FROM FROM
rlarp.price_pool p rlarp.price_pool_dev p
WHERE WHERE
gset @> jsonb_build_object( gset @> jsonb_build_object(
'mold', (SELECT doc->>'mold' FROM getj), 'mold', (SELECT doc->>'mold' FROM getj),

View File

@ -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 ,row_number() OVER (PARTITION BY p.agglevel ORDER BY avgunits DESC) rn
,stats.* ,stats.*
FROM FROM
rlarp.price_pool p rlarp.price_pool_dev p
CROSS JOIN sel CROSS JOIN sel
JOIN LATERAL jsonb_to_record(gset) AS gset( JOIN LATERAL jsonb_to_record(gset) AS gset(
chan text chan text

View File

@ -0,0 +1,69 @@
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;

View File

@ -46,7 +46,7 @@ lq AS MATERIALIZED (
,seas.* ,seas.*
FROM FROM
lqg g lqg g
LEFT OUTER JOIN rlarp.price_pool p ON LEFT OUTER JOIN rlarp.price_pool_dev p ON
p.gset @> jsonb_build_object('mold',g.part_group) p.gset @> jsonb_build_object('mold',g.part_group)
AND p.gset ? 'cust' AND p.gset ? 'cust'
AND p.gset ? 'v1ds' AND p.gset ? 'v1ds'

59
sql/livequotes_dev.pg.sql Normal file
View File

@ -0,0 +1,59 @@
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

View File

@ -2,8 +2,8 @@
--LANGUAGE plpgsql AS --LANGUAGE plpgsql AS
--$func$ --$func$
--BEGIN; --BEGIN;
-- DROP MATERIALIZED VIEW IF EXISTS rlarp.price_pool CASCADE; DROP MATERIALIZED VIEW IF EXISTS rlarp.price_pool CASCADE;
--CREATE TABLE IF NOT EXISTS rlarp.price_pool AS ( --CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS (
CREATE MATERIALIZED VIEW rlarp.price_pool AS ( CREATE MATERIALIZED VIEW rlarp.price_pool AS (
WITH WITH
agg AS ( agg AS (
@ -12,13 +12,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool AS (
,substring(o.version,1,1) version ,substring(o.version,1,1) version
,o.chanwide ,o.chanwide
,o.nursery_region ,o.nursery_region
,o.greenhouse_region ,c.greenhouse_region
,o.partgroup baseitem ,m.part_group baseitem
,o.majg ,m.majg
,o.assc ,m.assc
,o.coltier ,i.coltier
,o.dataseg v1dataseg ,'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
,o.v0dataseg v0dataseg ,_ds.dataseg v0dataseg
,o.oseas oseas ,o.oseas oseas
,round(sum(o.qty),0) units ,round(sum(o.qty),0) units
,round(sum(o.sales_usd),0) sales_usd ,round(sum(o.sales_usd),0) sales_usd
@ -26,26 +26,35 @@ CREATE MATERIALIZED VIEW rlarp.price_pool AS (
,count(DISTINCT o.ordnum) ordcount ,count(DISTINCT o.ordnum) ordcount
FROM FROM
rlarp.osm_stack o 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 LEFT OUTER JOIN pricequote.market_setavgprice tp ON
tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date)
AND tp.country = 'ALL' AND tp.country = 'ALL'
AND tp.geo = 'ALL' AND tp.geo = 'ALL'
AND tp.region = 'ALL' AND tp.region = 'ALL'
AND tp.mold = o.stlc AND tp.mold = i.stlc
AND tp.chan = 'DISTRIB DROP SHIP' AND tp.chan = 'DISTRIB DROP SHIP'
AND tp.data_segment = o.dataseg 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, ''))
LEFT OUTER JOIN pricequote.market_setavgprice tq ON LEFT OUTER JOIN pricequote.market_setavgprice tq ON
tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date)
AND tq.country = 'ALL' AND tq.country = 'ALL'
AND tq.geo = 'ALL' AND tq.geo = 'ALL'
AND tq.region = 'ALL' AND tq.region = 'ALL'
AND tq.mold = o.stlc AND tq.mold = i.stlc
AND tq.chan = 'DISTRIB DROP SHIP' AND tq.chan = 'DISTRIB DROP SHIP'
AND tq.data_segment = o.v0dataseg 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
WHERE WHERE
o.version IN ('Actual','Quotes') o.version IN ('Actual','Quotes')
AND o.oseas >= 2015 AND o.oseas >= 2015
AND o.ming <> 'B52' AND o.dsm <> 'PW'
--AND o.part like 'SQL035%' --AND o.part like 'SQL035%'
--AND o.calc_status <> 'CANCELED' --AND o.calc_status <> 'CANCELED'
--AND o.fs_line = '41010' --AND o.fs_line = '41010'
@ -56,13 +65,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool AS (
,substring(o.version,1,1) ,substring(o.version,1,1)
,o.chanwide ,o.chanwide
,o.nursery_region ,o.nursery_region
,o.greenhouse_region ,c.greenhouse_region
,o.partgroup ,m.part_group
,o.majg ,m.majg
,o.assc ,m.assc
,o.coltier ,i.coltier
,o.dataseg ,'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, ''))
,o.v0dataseg ,_ds.dataseg
,o.oseas ,o.oseas
HAVING HAVING
round(sum(o.qty),0) > 0 round(sum(o.qty),0) > 0
@ -73,40 +82,49 @@ CREATE MATERIALIZED VIEW rlarp.price_pool AS (
,substring(o.version,1,1) version ,substring(o.version,1,1) version
,o.chanwide ,o.chanwide
,o.nursery_region ,o.nursery_region
,o.greenhouse_region ,c.greenhouse_region
,o.partgroup baseitem ,m.part_group baseitem
,o.majg ,m.majg
,o.assc ,m.assc
,o.coltier ,i.coltier
,o.dataseg v1dataseg ,'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
,o.v0dataseg v0dataseg ,_ds.dataseg v0dataseg
,o.oseas oseas ,0 oseas
,round(sum(o.qty),0) units ,round(sum(o.qty),0) units
,round(sum(o.sales_usd),0) sales_usd ,round(sum(o.sales_usd),0) sales_usd
,round(sum(COALESCE(tp.target_price,tq.target_price) * o.qty),2) target_price ,round(sum(COALESCE(tp.target_price,tq.target_price) * o.qty),2) target_price
,count(DISTINCT o.ordnum) ordcount ,count(DISTINCT o.ordnum) ordcount
FROM FROM
rlarp.osm_stack o 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 LEFT OUTER JOIN pricequote.market_setavgprice tp ON
tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) tp.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date)
AND tp.country = 'ALL' AND tp.country = 'ALL'
AND tp.geo = 'ALL' AND tp.geo = 'ALL'
AND tp.region = 'ALL' AND tp.region = 'ALL'
AND tp.mold = o.stlc AND tp.mold = i.stlc
AND tp.chan = 'DISTRIB DROP SHIP' AND tp.chan = 'DISTRIB DROP SHIP'
AND tp.data_segment = o.dataseg 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, ''))
LEFT OUTER JOIN pricequote.market_setavgprice tq ON LEFT OUTER JOIN pricequote.market_setavgprice tq ON
tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date) tq.season = (SELECT ssyr FROM rlarp.gld where drange @> current_date)
AND tq.country = 'ALL' AND tq.country = 'ALL'
AND tq.geo = 'ALL' AND tq.geo = 'ALL'
AND tq.region = 'ALL' AND tq.region = 'ALL'
AND tq.mold = o.stlc AND tq.mold = i.stlc
AND tq.chan = 'DISTRIB DROP SHIP' AND tq.chan = 'DISTRIB DROP SHIP'
AND tq.data_segment = o.v0dataseg 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
WHERE WHERE
o.version IN ('Actual','Quotes') o.version IN ('Actual','Quotes')
AND o.odate >= current_date - '4 months'::interval AND o.odate >= current_date - '4 months'::interval
AND o.ming <> 'B52' AND o.dsm <> 'PW'
--AND o.part like 'SQL035%' --AND o.part like 'SQL035%'
--AND o.calc_status <> 'CANCELED' --AND o.calc_status <> 'CANCELED'
--AND o.fs_line = '41010' --AND o.fs_line = '41010'
@ -117,13 +135,13 @@ CREATE MATERIALIZED VIEW rlarp.price_pool AS (
,substring(o.version,1,1) ,substring(o.version,1,1)
,o.chanwide ,o.chanwide
,o.nursery_region ,o.nursery_region
,o.greenhouse_region ,c.greenhouse_region
,o.partgroup ,m.part_group
,o.majg ,m.majg
,o.assc ,m.assc
,o.coltier ,i.coltier
,o.dataseg ,'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, ''))
,o.v0dataseg ,_ds.dataseg
,o.oseas ,o.oseas
HAVING HAVING
round(sum(o.qty),0) > 0 round(sum(o.qty),0) > 0