vault backup: 2023-11-10 01:44:37

This commit is contained in:
Paul Trowbridge 2023-11-10 01:44:37 -05:00
parent c0324bb227
commit 4c79a1ec5f
7 changed files with 465 additions and 173 deletions

View File

@ -16,135 +16,135 @@ export function apply_guidance(doc: any) {
let mostRelevantCustomerSource = null; let mostRelevantCustomerSource = null;
// Function to update price and assign relevance indicator // Function to update price and assign relevance indicator
function setAnchors(items, channelFirstChar, v1ds, v0ds, histKey) { //function setAnchors(items, channelFirstChar, v1ds, v0ds, histKey) {
for (let item of items) { // for (let item of items) {
// Update the last_price with the most recent price // // Update the last_price with the most recent price
const years = Object.keys(item.season).map(Number).filter(year => year >= 2020); // const years = Object.keys(item.season).map(Number).filter(year => year >= 2020);
if (years.length > 0) { // if (years.length > 0) {
const recentYear = Math.max(...years.map(Number)); // const recentYear = Math.max(...years.map(Number));
const earlyYear = Math.min(...years.map(Number)); // const earlyYear = Math.min(...years.map(Number));
const lastPrice = item.season[recentYear].price_usd; // const lastPrice = item.season[recentYear].price_usd;
const earlyPrice = item.season[earlyYear].price_usd; // const earlyPrice = item.season[earlyYear].price_usd;
item.last_price = lastPrice; // item.last_price = lastPrice;
item.early_price = earlyPrice; // item.early_price = earlyPrice;
item.last_season = recentYear; // item.last_season = recentYear;
item.early_season = earlyYear; // item.early_season = earlyYear;
} else { // } else {
item.last_price = null; // or some default value as appropriate // item.last_price = null; // or some default value as appropriate
} // }
// Initialize relevance as numeric value // // Initialize relevance as numeric value
let marketRelevance = 0; // Assume 0 is 'not relevant' // let marketRelevance = 0; // Assume 0 is 'not relevant'
let customerRelevance = 0; // Assume 0 is 'not relevant' // let customerRelevance = 0; // Assume 0 is 'not relevant'
// Check if the first character of the item's channel matches the first character of the document's channel // // Check if the first character of the item's channel matches the first character of the document's channel
if (item.chan.charAt(0) === channelFirstChar) { // if (item.chan.charAt(0) === channelFirstChar) {
marketRelevance = 1; // 'relevant' // marketRelevance = 1; // 'relevant'
// Further refine relevance based on v1ds and v0ds // // Further refine relevance based on v1ds and v0ds
if (item.v1ds === v1ds) { // if (item.v1ds === v1ds) {
marketRelevance = 2; // 'most relevant' because v1ds matches // marketRelevance = 2; // 'most relevant' because v1ds matches
// Check for customer relevance if 'cust' key exists // // Check for customer relevance if 'cust' key exists
customerRelevance = item.cust ? 3 : 0; // customerRelevance = item.cust ? 3 : 0;
} else if (item.v0ds === v0ds) { // } else if (item.v0ds === v0ds) {
marketRelevance = marketRelevance === 2 ? 2 : 1; // Keep relevance as is if v1ds was matched, otherwise it's just 'relevant' // marketRelevance = marketRelevance === 2 ? 2 : 1; // Keep relevance as is if v1ds was matched, otherwise it's just 'relevant'
customerRelevance = item.cust ? 2 : 0; // customerRelevance = item.cust ? 2 : 0;
} else if (item.cust) { // } else if (item.cust) {
customerRelevance = item.v1ds ? 2 : item.v0ds ? 1 : 0; // customerRelevance = item.v1ds ? 2 : item.v0ds ? 1 : 0;
} // }
} // }
// Assign the calculated relevance to the item // // Assign the calculated relevance to the item
item.marketRelevance = marketRelevance; // item.marketRelevance = marketRelevance;
item.customerRelevance = customerRelevance; // item.customerRelevance = customerRelevance;
// Update the most relevant market price if this item's relevance is higher and it doesn't have a 'cust' key // // Update the most relevant market price if this item's relevance is higher and it doesn't have a 'cust' key
if (marketRelevance > highestMarketRelevanceLevel) { // if (marketRelevance > highestMarketRelevanceLevel) {
highestMarketRelevanceLevel = marketRelevance; // highestMarketRelevanceLevel = marketRelevance;
mostRelevantMarketPrice = item.last_price; // mostRelevantMarketPrice = item.last_price;
mostRelevantMarketPriceEarly = item.early_price; // mostRelevantMarketPriceEarly = item.early_price;
mostRelevantMarketKey = histKey; // mostRelevantMarketKey = histKey;
mostRelevantMarketSource = item; // mostRelevantMarketSource = item;
delete mostRelevantMarketSource.season; // delete mostRelevantMarketSource.season;
mostRelevantMarketSeason = item.last_season; // Assuming 'season' is the key where the season info is stored // mostRelevantMarketSeason = item.last_season; // Assuming 'season' is the key where the season info is stored
mostRelevantMarketSeasonEarly = item.early_season; // mostRelevantMarketSeasonEarly = item.early_season;
} // }
// Update the most relevant customer price if this item's relevance is higher and it has a 'cust' key // // Update the most relevant customer price if this item's relevance is higher and it has a 'cust' key
if (customerRelevance > highestCustomerRelevanceLevel) { // if (customerRelevance > highestCustomerRelevanceLevel) {
highestCustomerRelevanceLevel = customerRelevance; // highestCustomerRelevanceLevel = customerRelevance;
mostRelevantCustomerPriceRecent = item.last_price; // mostRelevantCustomerPriceRecent = item.last_price;
mostRelevantCustomerPriceEarly = item.early_price; // mostRelevantCustomerPriceEarly = item.early_price;
mostRelevantCustomerKey = histKey; // mostRelevantCustomerKey = histKey;
mostRelevantCustomerSource = item; // mostRelevantCustomerSource = item;
delete mostRelevantCustomerSource.season; // delete mostRelevantCustomerSource.season;
mostRelevantCustomerSeasonRecent = item.last_season; // Assuming 'season' is the key where the season info is stored // mostRelevantCustomerSeasonRecent = item.last_season; // Assuming 'season' is the key where the season info is stored
mostRelevantCustomerSeasonEarly = item.early_season; // Assuming 'season' is the key where the season info is stored // mostRelevantCustomerSeasonEarly = item.early_season; // Assuming 'season' is the key where the season info is stored
} // }
} // }
} //}
// Iterate over each key in the "hist" object //// Iterate over each key in the "hist" object
for (let key of Object.keys(doc.hist)) { //for (let key of Object.keys(doc.hist)) {
// Update price and relevance for each item in the current key // // Update price and relevance for each item in the current key
setAnchors(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key); // setAnchors(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key);
} //}
// Assign the most relevant market price and key to the top level of the document //// Assign the most relevant market price and key to the top level of the document
if (mostRelevantMarketPrice !== null) { //if (mostRelevantMarketPrice !== null) {
doc.mostRelevantMarketPriceInfo = { // doc.mostRelevantMarketPriceInfo = {
price: mostRelevantMarketPrice, // price: mostRelevantMarketPrice,
price_early: mostRelevantMarketPriceEarly, // price_early: mostRelevantMarketPriceEarly,
source: mostRelevantMarketSource, // source: mostRelevantMarketSource,
season: mostRelevantMarketSeason, // season: mostRelevantMarketSeason,
season_early: mostRelevantMarketSeasonEarly, // season_early: mostRelevantMarketSeasonEarly,
relevance: highestMarketRelevanceLevel // relevance: highestMarketRelevanceLevel
}; // };
} //}
// Assign the most relevant customer price and key to the top level of the document //// Assign the most relevant customer price and key to the top level of the document
if (mostRelevantCustomerPriceRecent !== null) { //if (mostRelevantCustomerPriceRecent !== null) {
doc.mostRelevantCustomerPriceInfo = { // doc.mostRelevantCustomerPriceInfo = {
price: mostRelevantCustomerPriceRecent, // price: mostRelevantCustomerPriceRecent,
price_early: mostRelevantCustomerPriceEarly, // price_early: mostRelevantCustomerPriceEarly,
source: mostRelevantCustomerSource, // source: mostRelevantCustomerSource,
season: mostRelevantCustomerSeasonRecent, // season: mostRelevantCustomerSeasonRecent,
season_early: mostRelevantCustomerSeasonEarly, // season_early: mostRelevantCustomerSeasonEarly,
relevance: highestCustomerRelevanceLevel // relevance: highestCustomerRelevanceLevel
}; // };
} //}
doc.targetPrice = doc.v1tp ?? doc.v0tp ?? null; //doc.targetPrice = doc.v1tp ?? doc.v0tp ?? null;
// Determine the anchor price and source //// Determine the anchor price and source
if (doc.targetPrice !== undefined && (mostRelevantCustomerPriceEarly === undefined || doc.targetPrice < mostRelevantCustomerPriceEarly)) { //if (doc.targetPrice !== undefined && (mostRelevantCustomerPriceEarly === undefined || doc.targetPrice < mostRelevantCustomerPriceEarly)) {
doc.anchorPrice = doc.targetPrice; // doc.anchorPrice = doc.targetPrice;
doc.anchorSource = 'Target Price'; // doc.anchorSource = 'Target Price';
} else if (mostRelevantCustomerPriceEarly !== undefined) { //} else if (mostRelevantCustomerPriceEarly !== undefined) {
doc.anchorPrice = mostRelevantCustomerPriceEarly; // doc.anchorPrice = mostRelevantCustomerPriceEarly;
doc.anchorSource = mostRelevantCustomerSeasonEarly + ' Customer Price'; // doc.anchorSource = mostRelevantCustomerSeasonEarly + ' Customer Price';
} else { //} else {
doc.anchorPrice = null; // doc.anchorPrice = null;
doc.anchorSource = 'none'; // or any other default value you wish to indicate no anchor price was found // doc.anchorSource = 'none'; // or any other default value you wish to indicate no anchor price was found
} //}
const inflation = Math.max(...Object.keys(doc.iidx).map(Number)); //const inflation = Math.max(...Object.keys(doc.iidx).map(Number));
const inflationFactor = doc.iidx[inflation] + 1; //const inflationFactor = doc.iidx[inflation] + 1;
doc.inflationFactor = inflationFactor; //doc.inflationFactor = inflationFactor;
var calcPrice = doc.anchorPrice * doc.inflationFactor; //var calcPrice = doc.anchorPrice * doc.inflationFactor;
calcPrice = parseFloat(calcPrice.toFixed(5)); //calcPrice = parseFloat(calcPrice.toFixed(5));
let finalReason = ""; //let finalReason = "";
if (calcPrice >= doc.list) { //if (calcPrice >= doc.list) {
doc.calcCeiling = "Cap At List"; // doc.calcCeiling = "Cap At List";
doc.finalPrice = doc.list; // doc.finalPrice = doc.list;
finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; // finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`;
} else { //} else {
doc.finalPrice = calcPrice; // doc.finalPrice = calcPrice;
finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice}`; // finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice}`;
} //}
doc.finalReason = finalReason; //doc.finalReason = finalReason;
return doc; return doc;
} }

View File

@ -62,7 +62,7 @@ BEGIN
--RAISE NOTICE 'cust %', _cust; --RAISE NOTICE 'cust %', _cust;
----------------price history------------------------------- ----------------price history-------------------------------
SELECT _rslt||jsonb_build_object('hist',rlarp.gethist(_mold, _cust)) INTO _rslt ; SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt ;
--RAISE NOTICE '%', _rslt; --RAISE NOTICE '%', _rslt;
----------------target pricing------------------------------ ----------------target pricing------------------------------
@ -127,12 +127,12 @@ BEGIN
priority; priority;
_rslt := _rslt||COALESCE(_iidx,'{}'::jsonb); _rslt := _rslt||COALESCE(_iidx,'{}'::jsonb);
----------------list ppricing------------------------------- ----------------list pricing--------------------------------
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
_rslt := _rslt||_list; _rslt := _rslt||_list;
--RAISE NOTICE 'list: %', jsonb_pretty(_list); --RAISE NOTICE 'list: %', jsonb_pretty(_list);
----------------list ppricing------------------------------- ----------------get premium for quote hist gap--------------
SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_v1ds, ((_rslt->'mostRelevantCustomerPriceInfo')->'source')->>'v1ds'),'{}'::jsonb) INTO _prem; SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_v1ds, ((_rslt->'mostRelevantCustomerPriceInfo')->'source')->>'v1ds'),'{}'::jsonb) INTO _prem;
_rslt := _rslt||_prem; _rslt := _rslt||_prem;
--RAISE NOTICE 'list: %', jsonb_pretty(_list); --RAISE NOTICE 'list: %', jsonb_pretty(_list);

View File

@ -62,23 +62,23 @@ BEGIN
,i.v0ds ,i.v0ds
,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)); ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier));
_rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc)||_iidx; _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc)||_iidx;
--RAISE NOTICE 'item data %', _iidx; RAISE NOTICE 'item data %', _iidx;
----------------channel------------------------------------- ----------------channel-------------------------------------
SELECT rlarp.channel_code(_bill, _ship) INTO _chan; SELECT rlarp.channel_code(_bill, _ship) INTO _chan;
_rslt := _rslt||jsonb_build_object('chan',_chan); _rslt := _rslt||jsonb_build_object('chan',_chan);
--RAISE NOTICE '%', _chan; RAISE NOTICE 'chan %', _chan;
----------------customer------------------------------------ ----------------customer------------------------------------
SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
_rslt = _rslt||jsonb_build_object('cust',_cust); _rslt = _rslt||jsonb_build_object('cust',_cust);
--RAISE NOTICE 'cust %', _cust; RAISE NOTICE 'cust %', _cust;
----------------price history------------------------------- ----------------price history-------------------------------
SELECT _rslt||jsonb_build_object('hist',rlarp.gethist(_mold, _cust)) INTO _rslt ; SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt ;
--RAISE NOTICE '%', _rslt; RAISE NOTICE 'result %', _rslt;
----------------target pricing------------------------------ ----------------target pricing------------------------------
SELECT SELECT
@ -141,11 +141,17 @@ BEGIN
GROUP BY GROUP BY
priority; priority;
_rslt := _rslt||COALESCE(_iidx,'{}'::jsonb); _rslt := _rslt||COALESCE(_iidx,'{}'::jsonb);
----------------list ppricing-------------------------------
----------------list pricing---------------------------------
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
_rslt := _rslt||_list; _rslt := _rslt||_list;
--RAISE NOTICE 'list: %', jsonb_pretty(_list); --RAISE NOTICE 'list: %', jsonb_pretty(_list);
----------------get premium for quote hist gap--------------
--SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_v1ds, ((_rslt->'mostRelevantCustomerPriceInfo')->'source')->>'v1ds'),'{}'::jsonb) INTO _prem;
--_rslt := _rslt||_prem;
--RAISE NOTICE 'list: %', jsonb_pretty(_list);
RETURN _rslt; RETURN _rslt;
END; END;

151
sql/get_hist.pg.sql Normal file
View File

@ -0,0 +1,151 @@
CREATE OR REPLACE FUNCTION rlarp.get_hist(_mold text, _v1ds text, _cust text, _chan text)
RETURNS jsonb
LANGUAGE plpgsql
AS $func$
DECLARE
_result jsonb;
BEGIN
WITH
--sel AS (select 'v1:P.P.PLT..' _v1ds, 'ALTMAN PLANTS' _cust, 'XNT0TQT3' _mold, 'D' _chan)
sort AS (
SELECT
p.agglevel
,CASE WHEN p.agglevel ? 'cust' THEN 'cust' ELSE 'market' END source
,COALESCE(gset.v1ds = _v1ds,false) v1ds_match
,gset.chan = _chan chan_match
,gset.*
,row_number() OVER (PARTITION BY p.agglevel ORDER BY avgunits DESC) rn
,stats.*
FROM
rlarp.price_pool_dev p
JOIN LATERAL jsonb_to_record(gset) AS gset(
chan text
,mold text
,v1ds text
,v0ds text
,cust text
,vers text
--,nurs text
--,ghse text
) ON TRUE
JOIN LATERAL jsonb_to_record(stats) AS stats(
avgunits numeric
,avgtargetprice numeric
,avgordcount numeric
,avgcustcount numeric
,early_season int
,early_price numeric
,recent_season int
,recent_price numeric
) ON TRUE
WHERE
gset @> jsonb_build_object(
'mold', _mold
,'vers', 'A'
)
AND (
gset @> jsonb_build_object(
'cust', _cust
)
OR NOT gset ? 'cust'
)
AND COALESCE(stats.early_season,stats.recent_season) IS NOT NULL
AND NOT p.agglevel ? 'nurs'
AND NOT p.agglevel ? 'ghse'
ORDER BY
source ASC
,rn ASC
)
,flag AS (
SELECT
--agglevel
CASE source
WHEN 'cust' THEN
CASE WHEN v1ds IS NOT NULL THEN
CASE WHEN v1ds_match THEN 'customer exact' ELSE
CASE WHEN rn = 1 THEN 'customer v1ds vol' ELSE 'customer v1ds other' END
END
ELSE
CASE WHEN rn = 1 THEN 'customer v0ds vol' ELSE 'customer v0ds other' END
END
ELSE
CASE WHEN v1ds IS NOT NULL THEN
CASE WHEN v1ds_match THEN 'market exact' ELSE
CASE WHEN rn = 1 THEN 'market v1ds vol' ELSE 'market v1ds other' END
END
ELSE
CASE WHEN rn = 1 THEN 'market v0ds vol' ELSE 'market v0ds other' END
END
END relevance
,source
,v1ds_match
,chan_match
,chan
,mold
,v1ds
,v0ds
,cust
,vers
,rn
,avgunits
,avgordcount
,avgcustcount
,avgtargetprice
,early_season
,early_price
,recent_season
,recent_price
FROM
sort
)
,rel AS (
SELECT * FROM (values
('customer exact' ,1)
,('customer v0ds other',7)
,('customer v0ds vol' ,3)
,('customer v1ds other',6)
,('customer v1ds vol' ,2)
,('market exact' ,4)
,('market v0ds other' ,9)
,('market v0ds vol' ,5)
,('market v1ds other' ,8)
) x (flag,prefer)
)
,rel_sort AS (
SELECT
-- flag.relevance
--,flag.source
--,rel.prefer
--,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) best
jsonb_strip_nulls(
jsonb_build_object(
relevance
,jsonb_build_object(
'avgunits' ,avgunits
,'avgordcount' ,avgordcount
,'avgcustcount' ,avgcustcount
,'avgtargetprice' ,avgtargetprice
,'early_season' ,early_season
,'early_price' ,early_price
,'recent_season' ,recent_season
,'recent_price' ,recent_price
,'ds' ,COALESCE(v1ds,v0ds)
,'rank'
,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC)
)
)
) doc
FROM
flag
LEFT OUTER JOIN rel ON
rel.flag = flag.relevance
WHERE
relevance ~ 'vol|exact'
)
--SELECT jsonb_pretty(jsonb_agg(doc)) FROM rel_sort
SELECT jsonb_obj_aggc(doc) INTO _result FROM rel_sort;
RETURN _result;
END
$func$;

View File

@ -22,8 +22,10 @@ BEGIN
gset, gset,
(SELECT string_agg(ae.v, '.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) AS agglvl, (SELECT string_agg(ae.v, '.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) AS agglvl,
season, season,
stats,
(SELECT doc FROM getj) AS gdoc (SELECT doc FROM getj) AS gdoc
FROM rlarp.price_pool_dev p FROM
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),
@ -41,7 +43,7 @@ BEGIN
SELECT SELECT
jsonb_build_object( jsonb_build_object(
agg.agglvl, agg.agglvl,
jsonb_agg(gset || jsonb_build_object('season', season)) jsonb_agg(gset || jsonb_build_object('season', season,'stats',stats))
) AS data, ) AS data,
gdoc gdoc
FROM agg FROM agg

View File

@ -1,45 +1,141 @@
WITH WITH
getj AS ( sel AS (select 'v1:P.P.PLT..' _v1ds, 'ALTMAN PLANTS' _cust, 'TFR001G0' _mold, 'D' _chan)
SELECT ,sort AS (
(
SELECT
jsonb_build_object('mold',JSON_AGG(DISTINCT stlc)) doc
FROM
"CMS.CUSLG".itemm
WHERE
item ~ 'TUH10000A10B04'
) ||
(
SELECT
jsonb_build_object('cust',JSONB_AGG(DISTINCT c.dba))
FROM
rlarp.cust c
WHERE
c.dba ~ 'DIAMOND R'
) doc
)
,agg AS (
SELECT SELECT
gset p.agglevel
,(SELECT string_agg(ae.v,'.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) agglvl ,CASE WHEN p.agglevel ? 'cust' THEN 'cust' ELSE 'market' END source
,season ,COALESCE(gset.v1ds = (SELECT _v1ds FROM sel),false) v1ds_match
--,(select doc from getj) gdoc ,gset.chan = _chan chan_match
,gset.*
,row_number() OVER (PARTITION BY p.agglevel ORDER BY avgunits DESC) rn
,stats.*
FROM FROM
rlarp.price_pool_dev p rlarp.price_pool_dev p
WHERE CROSS JOIN sel
--gut the exact mold and actuals only JOIN LATERAL jsonb_to_record(gset) AS gset(
chan text
,mold text
,v1ds text
,v0ds text
,cust text
,vers text
--,nurs text
--,ghse text
) ON TRUE
JOIN LATERAL jsonb_to_record(stats) AS stats(
avgunits numeric
,avgtargetprice numeric
,avgordcount numeric
,avgcustcount numeric
,early_season int
,early_price numeric
,recent_season int
,recent_price numeric
) ON TRUE
WHERE
gset @> jsonb_build_object( gset @> jsonb_build_object(
'mold',(SELECT doc->'mold'->>0 FROM getj), 'mold', (SELECT _mold FROM sel)
'vers','A' ,'vers', 'A'
) )
--pull either the exact customer or no customer
AND ( AND (
gset @> jsonb_build_object( gset @> jsonb_build_object(
'cust',(SELECT doc->'cust'->>0 FROM getj) 'cust', (SELECT _cust FROM sel)
) )
OR NOT gset ? 'cust' OR NOT gset ? 'cust'
) )
ORDER BY AND COALESCE(stats.early_season,stats.recent_season) IS NOT NULL
agglevel ASC AND NOT p.agglevel ? 'nurs'
AND NOT p.agglevel ? 'ghse'
ORDER BY
source ASC
,rn ASC
) )
SELECT * FROM agg --,flag AS (
--SELECT
-- --agglevel
-- CASE source
-- WHEN 'cust' THEN
-- CASE WHEN v1ds IS NOT NULL THEN
-- CASE WHEN v1ds_match THEN 'customer exact' ELSE
-- CASE WHEN rn = 1 THEN 'customer v1ds vol' ELSE 'customer v1ds other' END
-- END
-- ELSE
-- CASE WHEN rn = 1 THEN 'customer v0ds vol' ELSE 'customer v0ds other' END
-- END
-- ELSE
-- CASE WHEN v1ds IS NOT NULL THEN
-- CASE WHEN v1ds_match THEN 'market exact' ELSE
-- CASE WHEN rn = 1 THEN 'market v1ds vol' ELSE 'market v1ds other' END
-- END
-- ELSE
-- CASE WHEN rn = 1 THEN 'market v0ds vol' ELSE 'market v0ds other' END
-- END
-- END relevance
-- ,source
-- ,v1ds_match
-- ,chan_match
-- ,chan
-- ,mold
-- ,v1ds
-- ,v0ds
-- ,cust
-- ,vers
-- ,rn
-- ,avgunits
-- ,avgordcount
-- ,avgcustcount
-- ,avgtargetprice
-- ,early_season
-- ,early_price
-- ,recent_season
-- ,recent_price
--FROM
-- sort
--)
--,rel AS (
-- SELECT * FROM (values
-- ('customer exact' ,1)
-- ,('customer v0ds other',7)
-- ,('customer v0ds vol' ,3)
-- ,('customer v1ds other',6)
-- ,('customer v1ds vol' ,2)
-- ,('market exact' ,4)
-- ,('market v0ds other' ,9)
-- ,('market v0ds vol' ,5)
-- ,('market v1ds other' ,8)
-- ) x (flag,prefer)
--)
--,rel_sort AS (
-- SELECT
-- -- flag.relevance
-- --,flag.source
-- --,rel.prefer
-- --,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) best
-- jsonb_strip_nulls(
-- jsonb_build_object(
-- relevance
-- ,jsonb_build_object(
-- 'avgunits' ,avgunits
-- ,'avgordcount' ,avgordcount
-- ,'avgcustcount' ,avgcustcount
-- ,'avgtargetprice' ,avgtargetprice
-- ,'early_season' ,early_season
-- ,'early_price' ,early_price
-- ,'recent_season' ,recent_season
-- ,'recent_price' ,recent_price
-- ,'ds' ,COALESCE(v1ds,v0ds)
-- ,'rank'
-- ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC)
-- )
-- )
-- ) doc
-- FROM
-- flag
-- LEFT OUTER JOIN rel ON
-- rel.flag = flag.relevance
-- WHERE
-- relevance ~ 'vol|exact'
--)
----SELECT jsonb_pretty(jsonb_agg(doc)) FROM rel_sort
----SELECT jsonb_pretty(jsonb_obj_aggc(doc)) FROM rel_sort
SELECT * FROM sort

View File

@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS (
,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
FROM FROM
rlarp.osm_stack o rlarp.osm_stack o
INNER JOIN "CMS.CUSLG".itemm i ON INNER JOIN "CMS.CUSLG".itemm i ON
@ -52,6 +53,7 @@ CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS (
WHERE WHERE
o.version IN ('Actual','Quotes') o.version IN ('Actual','Quotes')
AND o.oseas >= 2015 AND o.oseas >= 2015
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'
@ -93,8 +95,10 @@ CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS (
,sum(units ) units ,sum(units ) units
,sum(sales_usd) sales_usd ,sum(sales_usd) sales_usd
,round(sum(sales_usd )/sum(units),5) price_usd ,round(sum(sales_usd )/sum(units),5) price_usd
,round(sum(target_price)/sum(units),5) target_price ,round(sum(target_price)/sum(units) FILTER (WHERE COALESCE(target_price,0) <> 0 ),5) target_price
,jsonb_agg(DISTINCT coltier) coltier ,jsonb_agg(DISTINCT coltier) coltier
,count(DISTINCT customer) custcount
,sum(ordcount) ordcount
FROM FROM
agg agg
GROUP BY GROUP BY
@ -111,21 +115,54 @@ CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS (
,(oseas, version, baseitem, chanwide, v0dataseg, greenhouse_region ) ,(oseas, version, baseitem, chanwide, v0dataseg, greenhouse_region )
) )
) )
SELECT ,find_stats AS (
SELECT
gset
,jsonb_object_agg(oseas,jsonb_build_object('units',units,'sales_usd',sales_usd,'price_usd',price_usd,'target_price',target_price)) season
,(SELECT JSONB_AGG(k.v ORDER BY k.v ASC) FROM jsonb_object_keys(gset) k(v)) agglevel
,round(avg(custcount),1) avgcustcount
,round(avg(ordcount) ,1) avgordcount
,round(avg(units) ,0) avgunits
,round(avg(target_price),5) avgtargetprice
,min(oseas) FILTER (WHERE oseas BETWEEN 2020 AND 2022)::text early_season
,min(oseas) FILTER (WHERE oseas >= 2024)::text recent_season
--,oseas
--,units
--,sales_usd
--,price_usd
FROM
gsets
WHERE
oseas IS NOT NULL
--AND gset @> jsonb_build_object('mold', 'XNS0T1G3')
GROUP BY
gset
)
SELECT
gset gset
,jsonb_object_agg(oseas,jsonb_build_object('units',units,'sales_usd',sales_usd,'price_usd',price_usd,'target_price',target_price)) season ,season
,(SELECT JSONB_AGG(k.v ORDER BY k.v ASC) FROM jsonb_object_keys(gset) k(v)) agglevel ,agglevel
--,oseas ,jsonb_build_object(
--,units 'avgcustcount'
--,sales_usd ,avgcustcount
--,price_usd ,'avgordcount'
,avgordcount
,'avgunits'
,avgunits
,'avgtargetprice'
,avgtargetprice
,'early_season'
,early_season::int
,'early_price'
,(season->early_season->>'price_usd')::numeric
,'recent_season'
,recent_season::int
,'recent_price'
,(season->recent_season->>'price_usd')::numeric
) stats
FROM FROM
gsets find_stats
WHERE --LIMIT 1000
oseas IS NOT NULL
--AND gset @> jsonb_build_object('baseitem', 'XNS0T1G3')
GROUP BY
gset
) WITH DATA; ) WITH DATA;
create index ppd_gset on rlarp.price_pool_dev using gin (gset); create index ppd_gset on rlarp.price_pool_dev using gin (gset);