include last part code and add item for total target price

This commit is contained in:
Paul Trowbridge 2025-08-11 20:24:12 -04:00
parent 062b49e60c
commit eb7563f96c
4 changed files with 77 additions and 17 deletions

View File

@ -113,6 +113,7 @@ BEGIN
last_order NVARCHAR(10),
last_quote NVARCHAR(10),
last_isdiff NVARCHAR(100),
last_part NVARCHAR(100),
------------step 3 lookup target---------------
tprice NUMERIC(20,5),
tprice_last NUMERIC(20,5),
@ -225,10 +226,11 @@ BEGIN
last_order = b.ord,
last_quote = b.quote,
last_isdiff = CASE WHEN b.dataseg IS NOT NULL AND q.v1ds IS NOT NULL AND b.dataseg <> q.v1ds
THEN 'Last Sale Diff Part' ELSE '' END
THEN 'Last Sale Diff Part' ELSE '' END,
last_part = b.part
FROM @queue q
CROSS APPLY (
SELECT TOP 1 price, source, odate, qty, dataseg, ord, quote
SELECT TOP 1 price, source, odate, qty, dataseg, ord, quote, part
FROM pricing.pick_last_price_from_hist_json(q.hist, q.v1ds)
) b;
@ -392,6 +394,7 @@ BEGIN
,q.last_source AS last_source
,FORMAT(q.last_date, 'yyyy-MM-dd') AS last_date
,q.last_isdiff AS last_isdiff
,q.last_part AS last_part
,q.tprice_last AS tprice_last
,q.tprice AS target_price
,JSON_QUERY(q.tmath) AS target_math
@ -459,10 +462,10 @@ BEGIN
WHEN q.last_price IS NOT NULL THEN
CONCAT(
CASE ISNULL(q.last_source, '')
WHEN 'mrq' THEN 'Recent similar quote'
WHEN 'mra' THEN 'Recent similar sales'
WHEN 'mrq' THEN 'Recent similar ' + last_part
WHEN 'mrs' THEN 'Recent similar ' + last_part
WHEN 'dsq' THEN 'Last quote'
WHEN 'mrq' THEN 'Last sale'
WHEN 'dss' THEN 'Last sale'
ELSE ''
END,
CASE WHEN ISNULL(q.last_order, '0') = '0'
@ -494,7 +497,7 @@ BEGIN
-- Target Support Panel
SELECT
'Target Support' AS label,
'Target Calculation' AS label,
(
SELECT
----------------------label------------------------------------------------
@ -517,6 +520,26 @@ BEGIN
WITH (value NVARCHAR(MAX) '$')
FOR JSON PATH
) AS details
UNION ALL
-- Target Price
SELECT
'Target Total' AS label,
(
SELECT
----------------------label------------------------------------------------
'Price' AS label,
----------------------value------------------------------------------------
tprice AS value,
----------------------type-------------------------------------------------
'currency' AS type,
----------------------note-------------------------------------------------
'' AS note
FROM @queue q
FOR JSON PATH
) AS details
UNION ALL

View File

@ -127,10 +127,10 @@ DECLARE
_last_isdiff TEXT;
_last_part TEXT;
------------step 3 lookup target---------------
_tprice NUMERIC;
_tprice NUMERIC(20,5);
_tmath JSONB;
_volume_range TEXT;
_tprice_last NUMERIC;
_tprice_last NUMERIC(20,5);
------------step 4 normalize last price--------
_curstd NUMERIC;
_futstd NUMERIC;
@ -451,10 +451,10 @@ BEGIN
'type', 'currency',
'note', CASE WHEN _last_price IS NOT NULL THEN
CASE _last_source
WHEN 'mrq' THEN 'Recent similar quote'
WHEN 'mra' THEN 'Recent similar sales'
WHEN 'mrq' THEN 'Recent similar ' || _last_part || ' '
WHEN 'mrs' THEN 'Recent similar ' || _last_part || ' '
WHEN 'dsq' THEN 'Last quote'
WHEN 'mrq' THEN 'Last sale'
WHEN 'dss' THEN 'Last sale'
ELSE ''
END ||
CASE WHEN COALESCE(_last_order, '0') = '0' THEN ' Qt# ' || COALESCE(_last_quote, '') ELSE ' Ord# ' || COALESCE(_last_order, '') END
@ -474,9 +474,44 @@ BEGIN
)
),
jsonb_build_object(
'label', 'Target Support',
'label', 'Target Calculation',
'details',
COALESCE(_tmath, jsonb_build_object('label','No Target','value','','type','text','note',''))
(
SELECT
jsonb_agg(
jsonb_build_object(
----------------------label------------------------------------------------
'label',CASE WHEN value <> '' THEN RTRIM(SUBSTRING(value,1,18)) ELSE 'No Target' END,
----------------------value------------------------------------------------
'value',CASE WHEN value <> '' THEN
SUBSTRING(value,23,7)::NUMERIC(20,5) +
CASE SUBSTRING(value,19,1) WHEN '+' THEN 0 ELSE -1 END
ELSE
0
END,
----------------------type-------------------------------------------------
'type', CASE WHEN value <> '' THEN
CASE SUBSTRING(value,19,1)
WHEN '+' THEN 'currency'
ELSE 'Percent'
END
ELSE '' END,
----------------------note-------------------------------------------------
'note',CASE WHEN value <> '' THEN
CASE SUBSTRING(value,19,1)
WHEN '+' THEN 'Price'
ELSE 'Premium'
END
ELSE '' END
)
)
FROM jsonb_array_elements_text(COALESCE(_tmath,'{}'::jsonb)) ae
)
),
jsonb_build_object(
'label', 'Target Total',
'details',
jsonb_build_object('label','Price','value',_tprice,'type','currency','note','')
),
jsonb_build_object(
'label', 'Guidance',

View File

@ -121,7 +121,7 @@ ON #flagged(customer, partgroup, dataseg, version, part, qty, price, odate, ordn
-- Step 3.1: Explode all flags from the #flagged table
WITH exploded_flags AS (
SELECT
customer, partgroup, dataseg, version, part, qty, price, odate, ordnum, quoten,
customer, partgroup, part, dataseg, version, part, qty, price, odate, ordnum, quoten,
flag
FROM #flagged
CROSS APPLY (VALUES (f1), (f2), (f3), (f4), (f5), (f6)) AS f(flag)

View File

@ -7,6 +7,7 @@ WITH base AS (
SELECT
customer,
partgroup,
part,
dataseg,
version,
qtyord AS qty,
@ -72,11 +73,11 @@ flagged AS (
FROM ranked
WHERE
rn_mrs = 1 OR rn_mrq = 1 OR rn_lvs = 1 OR rn_lvq = 1 OR rn_dss = 1 OR rn_dsq = 1
),
)
--SELECT * FROM flagged WHERE customer = 'HYBELS' AND partgroup = 'HZP3E100'
exploded_flags AS (
,exploded_flags AS (
SELECT
customer, partgroup, dataseg, version, qty, price, odate, ordnum, quoten,
customer, partgroup, part, dataseg, version, qty, price, odate, ordnum, quoten,
unnest(ARRAY[f1, f2, f3, f4, f5, f6]) AS flag
FROM flagged
),
@ -89,6 +90,7 @@ serialized_flags AS (
jsonb_build_object(
'version', version,
'datasegment', dataseg,
'part', part,
'qty', qty,
'price', price,
'odate', odate,