split new orders amongst accounts reps
This commit is contained in:
		
							parent
							
								
									0d4901dc20
								
							
						
					
					
						commit
						8f0687fbb8
					
				| @ -1,3 +1,4 @@ | ||||
| CREATE MATERIALIZED VIEW rlarp.cust_review_basis AS ( | ||||
| WITH  | ||||
| act as ( | ||||
| SELECT  | ||||
| @ -94,9 +95,14 @@ GROUP BY | ||||
|         ,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) | ||||
|         ,COALESCE(act.dsm     ,TRIM(bgt.dsm     )) | ||||
| ) | ||||
| --SELECT * FROM agg; | ||||
| SELECT * FROM agg | ||||
| ); | ||||
| ------------------------------------------sales walk------------------------------------------------------- | ||||
| ,ask AS ( | ||||
| 
 | ||||
| DROP VIEW rlarp.cust_infered_forecast; | ||||
| CREATE VIEW rlarp.cust_infered_forecast AS ( | ||||
| WITH | ||||
| ask AS ( | ||||
| SELECT  | ||||
| 	 agg.bill_dba   | ||||
| 	,agg.ship_dba  | ||||
| @ -135,50 +141,116 @@ SELECT | ||||
| 	--,"Quotes" quotes | ||||
|     ,COALESCE(a.newords,0) newords | ||||
|     ,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 | ||||
| 	agg | ||||
| 	rlarp.cust_review_basis agg | ||||
| 	LEFT OUTER JOIN rlarp.customer_review a ON | ||||
| 		a.ship_cust = agg.ship_dba | ||||
| 		AND a.bill_cust = agg.bill_dba | ||||
| ) | ||||
| 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 | ||||
|     ,round(CASE a.newuom | ||||
|         WHEN 'Pallets' THEN a.newords * a.basis_ppl | ||||
|         WHEN 'Units' THEN a.newords * a.basis_ppu | ||||
|         WHEN 'Dollars' THEN a.newords | ||||
|     END,2) newdollars | ||||
|     ,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE | ||||
|         CASE a.newuom | ||||
|             WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp | ||||
|             WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp | ||||
|             WHEN 'Dollars' THEN a.newords / a.basis_ppp | ||||
|         END | ||||
|     END,2) newpounds | ||||
| FROM | ||||
|     ask a | ||||
| WHERE  | ||||
|     newords <> 0 | ||||
| ,infer 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.rep_count | ||||
|         ,round(CASE a.newuom | ||||
|             WHEN 'Pallets' THEN a.newords * a.basis_ppl | ||||
|             WHEN 'Units' THEN a.newords * a.basis_ppu | ||||
|             WHEN 'Dollars' THEN a.newords | ||||
|         END,2)*a.rep_count newdollars | ||||
|         ,ROUND(CASE WHEN COALESCE(a.basis_ppp,0) = 0 THEN NULL ELSE | ||||
|             CASE a.newuom | ||||
|                 WHEN 'Pallets' THEN (a.newords * a.basis_ppl) / a.basis_ppp | ||||
|                 WHEN 'Units' THEN (a.newords * a.basis_ppu) / a.basis_ppp | ||||
|                 WHEN 'Dollars' THEN a.newords / a.basis_ppp | ||||
|             END | ||||
|         END,2)*a.rep_count newpounds | ||||
|     FROM | ||||
|         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 | ||||
| ) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user