From c0324bb2277820eb97436bfdc2afcc8a6b2a2d29 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 9 Nov 2023 08:07:03 -0500 Subject: [PATCH] vault backup: 2023-11-09 08:07:03 --- sql/get_guidance.pg.sql | 7 ++++ sql/spread.pg.sql | 82 ++++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/sql/get_guidance.pg.sql b/sql/get_guidance.pg.sql index 3b90d31..6d2558d 100644 --- a/sql/get_guidance.pg.sql +++ b/sql/get_guidance.pg.sql @@ -22,6 +22,7 @@ DECLARE _rslt jsonb; _targ jsonb; _list jsonb; + _prem jsonb; _iidx jsonb; BEGIN @@ -125,11 +126,17 @@ BEGIN GROUP BY priority; _rslt := _rslt||COALESCE(_iidx,'{}'::jsonb); + ----------------list ppricing------------------------------- SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; _rslt := _rslt||_list; --RAISE NOTICE 'list: %', jsonb_pretty(_list); + ----------------list ppricing------------------------------- + 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; END; diff --git a/sql/spread.pg.sql b/sql/spread.pg.sql index 0c6105e..08ba5c3 100644 --- a/sql/spread.pg.sql +++ b/sql/spread.pg.sql @@ -1,68 +1,64 @@ DROP FUNCTION IF EXISTS rlarp.get_premium; -CREATE OR REPLACE FUNCTION rlarp.get_premium(_mold text, _season int, _chan text, _relevant text, _quoted text) +CREATE OR REPLACE FUNCTION rlarp.get_premium( + _mold text, + _season int, + _chan text, + _relevant text, + _quoted text +) RETURNS jsonb -LANGUAGE plpgsql AS ---DO -$func$ -BEGIN - +LANGUAGE plpgsql +AS $func$ DECLARE _factor jsonb; +BEGIN WITH ---targte spreads-- +--target spreads-- bridge AS ( SELECT - -- 'FHR14000' mold - --,2024 season - --,'DIRECT' chan - --,'v1:B..SLV..' relevant - --,'v1:S..SLV..' quoted - _mold mold - ,_season season - ,_chan chan - ,_relevant relevant - ,_quoted quoted -) -,ts AS ( + _mold AS mold + ,_season AS season + ,_chan AS chan + ,_relevant AS relevant + ,_quoted AS quoted +), +ts AS ( SELECT t.* - --, round(target_price/min(target_price) over (partition by mold, chan, season),5) spread - ,t.target_price/min(t.target_price) over (partition by t.mold, t.chan, t.season) spread + ,t.target_price / min(t.target_price) over (partition by t.mold, t.chan, t.season) AS spread ,b.quoted ,b.relevant - ,CASE t.data_segment WHEN b.quoted THEN 'get to here' WHEN b.relevant THEN 'from here' END flag + ,CASE + WHEN t.data_segment = b.quoted THEN 'get to here' + WHEN t.data_segment = b.relevant THEN 'from here' + END AS flag FROM pricequote.market_setavgprice t INNER JOIN bridge b ON - t.mold = b.mold - and t.season = b.season - and t.chan = b.chan - and t.data_segment like 'v1%' - and t.geo = 'ALL' - and t.country = 'ALL' - and t.geo = 'ALL' - ORDER BY - target_price ASC + t.mold = b.mold + AND t.season = b.season + AND t.chan = b.chan + AND t.data_segment LIKE 'v1%' + AND t.geo = 'ALL' + AND t.country = 'ALL' ) ---SELECT * FROM ts SELECT - --mold - --,chan - --,season - --,ROUND(max(spread) FILTER (WHERE flag = 'get to here'),5) quote - --,ROUND(max(spread) FILTER (WHERE flag = 'from here') ,5) relevant - jsonb_build_object('bridgePremium',ROUND(max(spread) FILTER (WHERE flag = 'get to here') - /max(spread) FILTER (WHERE flag = 'from here') ,5)) + jsonb_build_object( + 'bridgePremium', + ROUND(max(spread) FILTER (WHERE flag = 'get to here') / max(spread) FILTER (WHERE flag = 'from here'), 5) + ) INTO _factor FROM ts GROUP BY - mold - ,chan - ,season; + mold, chan, season; + +RAISE NOTICE 'relevant: %' , _relevant; +RAISE NOTICE 'quoted: %' , _quoted; RETURN _factor; END -$func$ +$func$; +