split new orders amongst accounts reps

This commit is contained in:
Paul Trowbridge 2023-12-18 22:56:46 -05:00
parent 0d4901dc20
commit 8f0687fbb8
1 changed files with 116 additions and 44 deletions

View File

@ -1,3 +1,4 @@
CREATE MATERIALIZED VIEW rlarp.cust_review_basis AS (
WITH WITH
act as ( act as (
SELECT SELECT
@ -94,9 +95,14 @@ GROUP BY
,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) ,COALESCE(act.ship_dba,TRIM(bgt.ship_dba))
,COALESCE(act.dsm ,TRIM(bgt.dsm )) ,COALESCE(act.dsm ,TRIM(bgt.dsm ))
) )
--SELECT * FROM agg; SELECT * FROM agg
);
------------------------------------------sales walk------------------------------------------------------- ------------------------------------------sales walk-------------------------------------------------------
,ask AS (
DROP VIEW rlarp.cust_infered_forecast;
CREATE VIEW rlarp.cust_infered_forecast AS (
WITH
ask AS (
SELECT SELECT
agg.bill_dba agg.bill_dba
,agg.ship_dba ,agg.ship_dba
@ -135,12 +141,22 @@ SELECT
--,"Quotes" quotes --,"Quotes" quotes
,COALESCE(a.newords,0) newords ,COALESCE(a.newords,0) newords
,COALESCE(a.newuom,'Units') newuom ,COALESCE(a.newuom,'Units') newuom
--,COUNT(*) OVER (PARTITION BY bill_dba, ship_dba) rep_count
--,sum(COALESCE((agg.pounds24 + agg.poundsop),0)) OVER (PARTITION BY bill_dba, ship_dba)/COALESCE((agg.pounds24 + agg.poundsop),0) rep_count
,ROUND(CASE WHEN
sum(COALESCE((agg.pounds24 + agg.poundsop),0)) OVER (PARTITION BY bill_dba, ship_dba) = 0 THEN
1::numeric/COUNT(*) OVER (PARTITION BY bill_dba, ship_dba)::numeric
ELSE
COALESCE((agg.pounds24 + agg.poundsop),0)
/sum(COALESCE((agg.pounds24 + agg.poundsop),0)) OVER (PARTITION BY bill_dba, ship_dba)
END,5) rep_count
FROM FROM
agg rlarp.cust_review_basis agg
LEFT OUTER JOIN rlarp.customer_review a ON LEFT OUTER JOIN rlarp.customer_review a ON
a.ship_cust = agg.ship_dba a.ship_cust = agg.ship_dba
AND a.bill_cust = agg.bill_dba AND a.bill_cust = agg.bill_dba
) )
,infer AS (
SELECT SELECT
a.bill_dba a.bill_dba
,a.ship_dba ,a.ship_dba
@ -166,19 +182,75 @@ SELECT
,a.basis_ppl ,a.basis_ppl
,a.newords ,a.newords
,a.newuom ,a.newuom
,a.rep_count
,round(CASE a.newuom ,round(CASE a.newuom
WHEN 'Pallets' THEN a.newords * a.basis_ppl WHEN 'Pallets' THEN a.newords * a.basis_ppl
WHEN 'Units' THEN a.newords * a.basis_ppu WHEN 'Units' THEN a.newords * a.basis_ppu
WHEN 'Dollars' THEN a.newords WHEN 'Dollars' THEN a.newords
END,2) newdollars END,2)*a.rep_count newdollars
,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE ,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE
CASE a.newuom CASE a.newuom
WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp
WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp
WHEN 'Dollars' THEN a.newords / a.basis_ppp WHEN 'Dollars' THEN a.newords / a.basis_ppp
END END
END,2) newpounds END,2)*a.rep_count newpounds
FROM FROM
ask a ask a
WHERE )
newords <> 0 ,limited AS (
SELECT
a.bill_dba
,a.ship_dba
,a.dsm
,a.salesbg
,a.sales23
,a.sales24
,a.salesop
,a.basis_ppp
,a.basis_ppu
,a.basis_ppl
,a.rep_count
,a.newords
,a.newuom
,a.newdollars
,a.newpounds
FROM
infer a
)
,alldata AS (
SELECT
a.bill_dba
,a.ship_dba
,a.dsm
,a.salesbg
,a.sales23
,a.sales24
,a.salesop
,a.poundsbg
,a.pounds23
,a.pounds24
,a.poundsop
,a.qtybg
,a.qty23
,a.qty24
,a.qtyop
,a.palletsbg
,a.pallets23
,a.pallets24
,a.palletsop
,a.basis_ppp
,a.basis_ppu
,a.basis_ppl
,a.newords
,a.newuom
,a.newdollars
,a.newpounds
,a.newdollars + sales24 + salesop fcsales24
,a.newpounds + pounds24 + poundsop fcpounds24
,a.rep_count
FROM
infer a
)
SELECT * FROM alldata
)