From 5234516dee27deffc57c6a7ca7e2279d9ce07bdc Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 23 Feb 2024 12:16:40 -0500 Subject: [PATCH] adjust history function --- sql/get_hist_debug.pg.sql | 54 ++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/sql/get_hist_debug.pg.sql b/sql/get_hist_debug.pg.sql index 67da137..8a0517d 100644 --- a/sql/get_hist_debug.pg.sql +++ b/sql/get_hist_debug.pg.sql @@ -1,5 +1,5 @@ WITH -sel AS (select 'v1:L..PLT..' _v1ds, 'GRIFFIN' _cust, 'ULH12000' _mold, 'W' _chan) +sel AS (select 'v1:T..PLT..' _v1ds, 'KAWAHARA NURSERY' _cust, 'AZE10001' _mold, 'D' _chan) ,sort AS ( SELECT p.agglevel @@ -53,6 +53,19 @@ sel AS (select 'v1:L..PLT..' _v1ds, 'GRIFFIN' _cust, 'ULH12000' _mold, 'W' _chan source ASC ,rn ASC ) +,rel AS ( + SELECT * FROM (values + ('customer exact' ,1) + ,('customer v0ds other',7) + ,('customer v0ds vol' ,3) + ,('customer v1ds other',6) + ,('customer v1ds vol' ,2) --this will always sort to the top, v0ds will never sort to the top. you will always be getting the highest volume base price + ,('market exact' ,4) + ,('market v0ds other' ,9) + ,('market v0ds vol' ,5) + ,('market v1ds other' ,8) + ) x (flag,prefer) +) ,flag AS ( SELECT --agglevel @@ -84,6 +97,7 @@ SELECT ,cust ,vers ,rn + ,row_number() OVER (PARTITION BY source ORDER BY rel.prefer ASC) rnk ,avgunits ,avgordcount ,avgcustcount @@ -97,19 +111,25 @@ SELECT ,season FROM sort -) -,rel AS ( - SELECT * FROM (values - ('customer exact' ,1) - ,('customer v0ds other',7) - ,('customer v0ds vol' ,3) - ,('customer v1ds other',6) - ,('customer v1ds vol' ,2) --this will always sort to the top, v0ds will never sort to the top. you will always be getting the highest volume base price - ,('market exact' ,4) - ,('market v0ds other' ,9) - ,('market v0ds vol' ,5) - ,('market v1ds other' ,8) - ) x (flag,prefer) + LEFT OUTER JOIN rel ON + rel.flag = CASE source + WHEN 'cust' THEN + CASE WHEN v1ds IS NOT NULL THEN + CASE WHEN v1ds_match THEN 'customer exact' ELSE + CASE WHEN rn = 1 THEN 'customer v1ds vol' ELSE 'customer v1ds other' END + END + ELSE + CASE WHEN rn = 1 THEN 'customer v0ds vol' ELSE 'customer v0ds other' END + END + ELSE + CASE WHEN v1ds IS NOT NULL THEN + CASE WHEN v1ds_match THEN 'market exact' ELSE + CASE WHEN rn = 1 THEN 'market v1ds vol' ELSE 'market v1ds other' END + END + ELSE + CASE WHEN rn = 1 THEN 'market v0ds vol' ELSE 'market v0ds other' END + END + END ) ,rel_sort AS ( SELECT @@ -133,17 +153,15 @@ FROM ,'last_season' ,last_season ,'last_price' ,last_price ,'ds' ,COALESCE(v1ds,v0ds) - ,'rank' ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) + ,'rank' ,rnk ,'pricinghistory' ,season ) ) ) doc - ,row_number() OVER (PARTITION BY flag.source ORDER BY rel.prefer ASC) rnk + ,rnk ,season FROM flag - LEFT OUTER JOIN rel ON - rel.flag = flag.relevance WHERE relevance ~ 'vol|exact' )