Compare commits

...

5 Commits

4 changed files with 70 additions and 61 deletions

View File

@ -548,7 +548,7 @@ BEGIN
-- Target Support Panel -- Target Support Panel
SELECT SELECT
'Target Calculation' AS label, 'Target Calculation' AS label,
10 AS detailLevel, 5 AS detailLevel,
( (
SELECT * FROM ( SELECT * FROM (
SELECT SELECT
@ -581,7 +581,7 @@ BEGIN
----------------------label------------------------------------------------ ----------------------label------------------------------------------------
'Target' AS label, 'Target' AS label,
----------------------detailLevel------------------------------------------ ----------------------detailLevel------------------------------------------
10 AS detailLevel, 5 AS detailLevel,
----------------------value------------------------------------------------ ----------------------value------------------------------------------------
tprice AS value, tprice AS value,
----------------------type------------------------------------------------- ----------------------type-------------------------------------------------

View File

@ -384,8 +384,8 @@ BEGIN
,_approval_price ,_approval_price
,_approval_reason ,_approval_reason
FROM FROM
pricequote.guidance_logic(_tprice, _last_price_norm, _listprice_eff, _last_date, .95, 1.0, 1.0) gl pricequote.guidance_logic(_tprice, _last_price_norm, _listprice_eff, _last_date, 1.0, 1.0, 1.0) gl
CROSS JOIN pricequote.approval_logic(_tprice, _last_price_norm, _listprice_eff, _last_date, .95, 1.0, 1.0) al; CROSS JOIN pricequote.approval_logic(_tprice, _last_price_norm, _listprice_eff, _last_date, 1.0, 1.0, 1.0) al;
------------------------------------------------------------------ ------------------------------------------------------------------
-- Step 8: Build explanation JSON -- Step 8: Build explanation JSON

View File

@ -51,42 +51,50 @@ WITH base AS (
ranked AS ( ranked AS (
SELECT SELECT
b.* b.*
-- Most recent sale -- Most recent sale (Actuals only)
,ROW_NUMBER() OVER ( ,CASE WHEN b.version = 'Actual' THEN
ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup PARTITION BY b.customer, b.partgroup
ORDER BY CASE WHEN b.version = 'Actual' THEN b.odate ELSE NULL END DESC ORDER BY b.odate DESC
) AS rn_mrs )
-- Most recent quote END AS rn_mrs
,ROW_NUMBER() OVER ( -- Most recent quote (Quotes only)
,CASE WHEN b.version = 'Quotes' THEN
ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup PARTITION BY b.customer, b.partgroup
ORDER BY CASE WHEN b.version = 'Quotes' THEN b.odate ELSE NULL END DESC ORDER BY b.odate DESC
) AS rn_mrq )
-- Largest volume sale (last 12 months) END AS rn_mrq
,ROW_NUMBER() OVER ( -- Largest volume sale (Actuals only; last 12 months prioritized)
,CASE WHEN b.version = 'Actual' THEN
ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup PARTITION BY b.customer, b.partgroup
ORDER BY CASE ORDER BY
WHEN b.version = 'Actual' AND b.odate >= DATEADD(YEAR, -1, GETDATE()) CASE WHEN b.version = 'Actual' AND b.odate >= DATEADD(YEAR, -1, GETDATE()) THEN 1 ELSE 0 END DESC,
THEN b.qty ELSE NULL b.qty DESC
END DESC )
) AS rn_lvs END AS rn_lvs
-- Largest volume quote (last 12 months) -- Largest volume quote (Quotes only; last 12 months prioritized)
,ROW_NUMBER() OVER ( ,CASE WHEN b.version = 'Quotes' THEN
ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup PARTITION BY b.customer, b.partgroup
ORDER BY CASE ORDER BY
WHEN b.version = 'Quotes' AND b.odate >= DATEADD(YEAR, -1, GETDATE()) CASE WHEN b.version = 'Quotes' AND b.odate >= DATEADD(YEAR, -1, GETDATE()) THEN 1 ELSE 0 END DESC,
THEN b.qty ELSE NULL b.qty DESC
END DESC )
) AS rn_lvq END AS rn_lvq
-- Most recent sale per data segment ,CASE WHEN b.version = 'Actual' THEN
,ROW_NUMBER() OVER ( ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup, b.dataseg, b.version PARTITION BY b.customer, b.partgroup, b.dataseg, b.version
ORDER BY CASE WHEN b.version = 'Actual' THEN b.odate ELSE NULL END DESC ORDER BY b.odate DESC
) AS rn_dss )
-- Most recent quote per data segment END AS rn_dss
,ROW_NUMBER() OVER ( ,CASE WHEN b.version = 'Quotes' THEN
ROW_NUMBER() OVER (
PARTITION BY b.customer, b.partgroup, b.dataseg, b.version PARTITION BY b.customer, b.partgroup, b.dataseg, b.version
ORDER BY CASE WHEN b.version = 'Quotes' THEN b.odate ELSE NULL END DESC ORDER BY b.odate DESC
) AS rn_dsq )
END AS rn_dsq
FROM base b FROM base b
) )
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -121,7 +129,7 @@ ON #flagged(customer, partgroup, dataseg, version, part, qty, price, odate, ordn
-- Step 3.1: Explode all flags from the #flagged table -- Step 3.1: Explode all flags from the #flagged table
WITH exploded_flags AS ( WITH exploded_flags AS (
SELECT SELECT
customer, partgroup, part, dataseg, version, part, qty, price, odate, ordnum, quoten, customer, partgroup, part, dataseg, version, qty, price, odate, ordnum, quoten,
flag flag
FROM #flagged FROM #flagged
CROSS APPLY (VALUES (f1), (f2), (f3), (f4), (f5), (f6)) AS f(flag) CROSS APPLY (VALUES (f1), (f2), (f3), (f4), (f5), (f6)) AS f(flag)

View File

@ -1,27 +1,28 @@
CREATE OR REPLACE PROCEDURE pricequote.refresh_target_prices_base()
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM pricequote.target_prices_base;
DELETE FROM pricequote.target_prices_base; WITH expand AS (
SELECT
WITH c.compset,
expand AS ( c.stlc,
SELECT c.floor,
c.compset, b.ds,
c.stlc, b.chan,
c.floor, b.tier,
b.ds, b.vol,
b.chan, b.val,
b.tier, b.price,
b.vol, b.math AS math
b.val, FROM pricequote.core_target c
b.price, LEFT JOIN LATERAL pricequote.build_pricing_path_base(
b.math math c.options || jsonb_build_object('entity','Anchor','attr',c.stlc,'val',c.floor,'func','Price')
FROM ) AS b
pricequote.core_target c ON b.lastflag
LEFT JOIN LATERAL pricequote.build_pricing_path_base (options||jsonb_build_object('entity','Anchor','attr',c.stlc,'val',c.floor,'func','Price')) b ON b.lastflag )
) INSERT INTO pricequote.target_prices_base
-- select count(*) from expand SELECT * FROM expand;
INSERT INTO END;
pricequote.target_prices_base $$;
SELECT
*
FROM
expand;