Compare commits

...

2 Commits

2 changed files with 35 additions and 44 deletions

View File

@ -35,40 +35,31 @@ BEGIN
END IF; END IF;
-- 1. Prefer the most recent of dss/dsq if either is within the age threshold -- 1. Prefer the most recent of dss/dsq if either is within the age threshold
IF (dsq_date IS NOT NULL AND dsq_date > (CURRENT_DATE - age_threshold)) IF dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold) THEN
OR (dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold)) THEN result := dss || jsonb_build_object('source', 'dss');
IF dsq_date IS NOT NULL AND (dss_date IS NULL OR dsq_date >= dss_date) AND dsq_date > (CURRENT_DATE - age_threshold) THEN
result := dsq || jsonb_build_object('source', 'dsq');
ELSIF dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold) THEN
result := dss || jsonb_build_object('source', 'dss');
END IF;
-- 2. If both dss/dsq are older than the threshold, use the most recent of mrs/mrq if either exists -- 2. If both dss/dsq are older than the threshold, use the most recent of mrs/mrq if either exists
ELSIF (mrq_date IS NOT NULL OR mrs_date IS NOT NULL) THEN ELSIF mrs_date IS NOT NULL AND mrs_date > (CURRENT_DATE - age_threshold) THEN
IF mrq_date IS NOT NULL AND (mrs_date IS NULL OR mrq_date >= mrs_date) THEN result := mrs || jsonb_build_object('source', 'mrs');
result := mrq || jsonb_build_object('source', 'mrq');
ELSIF mrs_date IS NOT NULL THEN
result := mrs || jsonb_build_object('source', 'mrs');
END IF;
-- 3. If all are at least as old as the threshold, pick the least oldest price available -- 3. If all are at least as old as the threshold, pick the least oldest price available
ELSE ELSE
best := NULL; best := NULL;
best_date := NULL; best_date := NULL;
IF dsq_date IS NOT NULL THEN -- IF dsq_date IS NOT NULL THEN
best := dsq || jsonb_build_object('source', 'dsq'); -- best := dsq || jsonb_build_object('source', 'dsq');
best_date := dsq_date; -- best_date := dsq_date;
END IF; -- END IF;
IF dss_date IS NOT NULL AND (best_date IS NULL OR dss_date > best_date) THEN -- IF dss_date IS NOT NULL AND (best_date IS NULL OR dss_date > best_date) THEN
best := dss || jsonb_build_object('source', 'dss'); -- best := dss || jsonb_build_object('source', 'dss');
best_date := dss_date; -- best_date := dss_date;
END IF; -- END IF;
IF mrq_date IS NOT NULL AND (best_date IS NULL OR mrq_date > best_date) THEN -- IF mrq_date IS NOT NULL AND (best_date IS NULL OR mrq_date > best_date) THEN
best := mrq || jsonb_build_object('source', 'mrq'); -- best := mrq || jsonb_build_object('source', 'mrq');
best_date := mrq_date; -- best_date := mrq_date;
END IF; -- END IF;
IF mrs_date IS NOT NULL AND (best_date IS NULL OR mrs_date > best_date) THEN -- IF mrs_date IS NOT NULL AND (best_date IS NULL OR mrs_date > best_date) THEN
best := mrs || jsonb_build_object('source', 'mrs'); -- best := mrs || jsonb_build_object('source', 'mrs');
best_date := mrs_date; -- best_date := mrs_date;
END IF; -- END IF;
result := best; result := best;
END IF; END IF;
RETURN result; RETURN result;

View File

@ -1,6 +1,6 @@
REFRESH MATERIALIZED VIEW pricequote.lastpricedetail; -- REFRESH MATERIALIZED VIEW pricequote.lastpricedetail;
--DROP MATERIALIZED VIEW pricequote.lastpricedetail DROP MATERIALIZED VIEW pricequote.lastpricedetail;
CREATE MATERIALIZED VIEW pricequote.lastpricedetail AS CREATE MATERIALIZED VIEW pricequote.lastpricedetail AS
WITH base AS ( WITH base AS (
@ -28,38 +28,38 @@ WITH base AS (
ranked AS ( ranked AS (
SELECT b.*, SELECT b.*,
-- Most recent sale (Actuals first, newest date first) -- Most recent sale (Actuals first, newest date first)
ROW_NUMBER() OVER ( CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup PARTITION BY customer, partgroup
ORDER BY (version = 'Actual') DESC, ORDER BY (version = 'Actual') DESC,
odate DESC NULLS LAST odate DESC NULLS LAST
) AS rn_mrs, ) END AS rn_mrs,
-- Most recent quote (Quotes first, newest date first) -- Most recent quote (Quotes first, newest date first)
ROW_NUMBER() OVER ( CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup PARTITION BY customer, partgroup
ORDER BY (version = 'Quotes') DESC, ORDER BY (version = 'Quotes') DESC,
odate DESC NULLS LAST odate DESC NULLS LAST
) AS rn_mrq, ) END AS rn_mrq,
-- Largest volume sale in last year (those inside window first) -- Largest volume sale in last year (those inside window first)
ROW_NUMBER() OVER ( CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup PARTITION BY customer, partgroup
ORDER BY (version = 'Actual' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC, ORDER BY (version = 'Actual' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC,
qty DESC NULLS LAST qty DESC NULLS LAST
) AS rn_lvs, ) END AS rn_lvs,
-- Largest volume quote in last year (those inside window first) -- Largest volume quote in last year (those inside window first)
ROW_NUMBER() OVER ( CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup PARTITION BY customer, partgroup
ORDER BY (version = 'Quotes' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC, ORDER BY (version = 'Quotes' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC,
qty DESC NULLS LAST qty DESC NULLS LAST
) AS rn_lvq, ) END AS rn_lvq,
-- Per dataseg/version: most recent (version fixed in partition, so just date) -- Per dataseg/version: most recent (version fixed in partition, so just date)
ROW_NUMBER() OVER ( CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup, dataseg, version PARTITION BY customer, partgroup, dataseg, version
ORDER BY odate DESC NULLS LAST ORDER BY odate DESC NULLS LAST
) AS rn_dss, ) END AS rn_dss,
ROW_NUMBER() OVER ( CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
PARTITION BY customer, partgroup, dataseg, version PARTITION BY customer, partgroup, dataseg, version
ORDER BY odate DESC NULLS LAST ORDER BY odate DESC NULLS LAST
) AS rn_dsq ) END AS rn_dsq
FROM base b FROM base b
), ),
flagged AS ( flagged AS (