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,50 +141,116 @@ 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
) )
SELECT ,infer AS (
a.bill_dba SELECT
,a.ship_dba a.bill_dba
,a.dsm ,a.ship_dba
,a.salesbg ,a.dsm
,a.sales23 ,a.salesbg
,a.sales24 ,a.sales23
,a.salesop ,a.sales24
,a.poundsbg ,a.salesop
,a.pounds23 ,a.poundsbg
,a.pounds24 ,a.pounds23
,a.poundsop ,a.pounds24
,a.qtybg ,a.poundsop
,a.qty23 ,a.qtybg
,a.qty24 ,a.qty23
,a.qtyop ,a.qty24
,a.palletsbg ,a.qtyop
,a.pallets23 ,a.palletsbg
,a.pallets24 ,a.pallets23
,a.palletsop ,a.pallets24
,a.basis_ppp ,a.palletsop
,a.basis_ppu ,a.basis_ppp
,a.basis_ppl ,a.basis_ppu
,a.newords ,a.basis_ppl
,a.newuom ,a.newords
,round(CASE a.newuom ,a.newuom
WHEN 'Pallets' THEN a.newords * a.basis_ppl ,a.rep_count
WHEN 'Units' THEN a.newords * a.basis_ppu ,round(CASE a.newuom
WHEN 'Dollars' THEN a.newords WHEN 'Pallets' THEN a.newords * a.basis_ppl
END,2) newdollars WHEN 'Units' THEN a.newords * a.basis_ppu
,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE WHEN 'Dollars' THEN a.newords
CASE a.newuom END,2)*a.rep_count newdollars
WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp ,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE
WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp CASE a.newuom
WHEN 'Dollars' THEN a.newords / a.basis_ppp WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp
END WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp
END,2) newpounds WHEN 'Dollars' THEN a.newords / a.basis_ppp
FROM END
ask a END,2)*a.rep_count newpounds
WHERE FROM
newords <> 0 ask a
)
,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
)