Compare commits
No commits in common. "2a699e8f8353e443352fb3aa301a76b7088cd49d" and "8974095341c8c437a4bafc3a727ff74f478efa0a" have entirely different histories.
2a699e8f83
...
8974095341
@ -35,31 +35,40 @@ BEGIN
|
||||
END IF;
|
||||
|
||||
-- 1. Prefer the most recent of dss/dsq if either is within the age threshold
|
||||
IF dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold) THEN
|
||||
IF (dsq_date IS NOT NULL AND dsq_date > (CURRENT_DATE - age_threshold))
|
||||
OR (dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold)) THEN
|
||||
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
|
||||
ELSIF mrs_date IS NOT NULL AND mrs_date > (CURRENT_DATE - age_threshold) THEN
|
||||
ELSIF (mrq_date IS NOT NULL OR mrs_date IS NOT NULL) THEN
|
||||
IF mrq_date IS NOT NULL AND (mrs_date IS NULL OR mrq_date >= mrs_date) THEN
|
||||
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
|
||||
ELSE
|
||||
best := NULL;
|
||||
best_date := NULL;
|
||||
-- IF dsq_date IS NOT NULL THEN
|
||||
-- best := dsq || jsonb_build_object('source', 'dsq');
|
||||
-- best_date := dsq_date;
|
||||
-- END IF;
|
||||
-- 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_date := dss_date;
|
||||
-- END IF;
|
||||
-- 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_date := mrq_date;
|
||||
-- END IF;
|
||||
-- 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_date := mrs_date;
|
||||
-- END IF;
|
||||
IF dsq_date IS NOT NULL THEN
|
||||
best := dsq || jsonb_build_object('source', 'dsq');
|
||||
best_date := dsq_date;
|
||||
END IF;
|
||||
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_date := dss_date;
|
||||
END IF;
|
||||
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_date := mrq_date;
|
||||
END IF;
|
||||
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_date := mrs_date;
|
||||
END IF;
|
||||
result := best;
|
||||
END IF;
|
||||
RETURN result;
|
||||
|
@ -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
|
||||
WITH base AS (
|
||||
@ -28,38 +28,38 @@ WITH base AS (
|
||||
ranked AS (
|
||||
SELECT b.*,
|
||||
-- Most recent sale (Actuals first, newest date first)
|
||||
CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup
|
||||
ORDER BY (version = 'Actual') DESC,
|
||||
odate DESC NULLS LAST
|
||||
) END AS rn_mrs,
|
||||
) AS rn_mrs,
|
||||
-- Most recent quote (Quotes first, newest date first)
|
||||
CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup
|
||||
ORDER BY (version = 'Quotes') DESC,
|
||||
odate DESC NULLS LAST
|
||||
) END AS rn_mrq,
|
||||
) AS rn_mrq,
|
||||
-- Largest volume sale in last year (those inside window first)
|
||||
CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup
|
||||
ORDER BY (version = 'Actual' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC,
|
||||
qty DESC NULLS LAST
|
||||
) END AS rn_lvs,
|
||||
) AS rn_lvs,
|
||||
-- Largest volume quote in last year (those inside window first)
|
||||
CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup
|
||||
ORDER BY (version = 'Quotes' AND odate >= CURRENT_DATE - INTERVAL '1 year') DESC,
|
||||
qty DESC NULLS LAST
|
||||
) END AS rn_lvq,
|
||||
) AS rn_lvq,
|
||||
-- Per dataseg/version: most recent (version fixed in partition, so just date)
|
||||
CASE WHEN version = 'Actual' THEN ROW_NUMBER() OVER (
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup, dataseg, version
|
||||
ORDER BY odate DESC NULLS LAST
|
||||
) END AS rn_dss,
|
||||
CASE WHEN version = 'Quotes' THEN ROW_NUMBER() OVER (
|
||||
) AS rn_dss,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, partgroup, dataseg, version
|
||||
ORDER BY odate DESC NULLS LAST
|
||||
) END AS rn_dsq
|
||||
) AS rn_dsq
|
||||
FROM base b
|
||||
),
|
||||
flagged AS (
|
||||
|
Loading…
Reference in New Issue
Block a user