update rep totals report

This commit is contained in:
Trowbridge 2020-02-19 17:15:43 -05:00
parent f924da0e4d
commit e9a0756240

View File

@ -1,47 +1,52 @@
WITH WITH
preagg AS ( preagg AS (
SELECT SELECT
director_descr coalesce(director,'None') director
,quota_rep_descr ,coalesce(quota_rep_descr,'None') quota_rep_descr
,part ,part
,bill_cust ,bill_cust_descr
,ship_cust ,ship_cust_descr
,order_month ,order_month
,iterdef ,doc->>'stamp' stamp
,sum(fb_val_loc * r_rate) FILTER (WHERE iter = 'copy') sales ,logid
,sum(fb_cst_loc * c_rate) FILTER (WHERE iter = 'copy') scogs ,sum(value_usd) FILTER (WHERE iter = 'copy') sales
,sum(fb_val_loc * r_rate) FILTER (WHERE iter <> 'copy') sales_adj ,sum(cost_usd) FILTER (WHERE iter = 'copy') scogs
,sum(fb_cst_loc * c_rate) FILTER (WHERE iter <> 'copy') scogs_adj ,sum(value_usd) FILTER (WHERE iter <> 'copy') sales_adj
,sum(cost_usd) FILTER (WHERE iter <> 'copy') scogs_adj
FROM FROM
rlarp.osm_fcpool rlarp.osm_pool
LEFT OUTER JOIN rlarp.osm_log ON
id = logid
WHERE WHERE
order_season = 2020 order_season = 2021
GROUP BY GROUP BY
director_descr director
,quota_rep_descr ,quota_rep_descr
,part ,part
,bill_cust ,bill_cust_descr
,ship_cust ,ship_cust_descr
,order_month ,order_month
,iterdef ,doc->>'stamp'
,logid
) )
SELECT SELECT
director_descr director
,quota_rep_descr ,quota_rep_descr
,to_char(sum(sales),'999,999,999') sales ,to_char(sum(sales),'999,999,999') sales
,to_char(sum(sales - scogs),'999,999,999') margin ,to_char(sum(sales - scogs),'999,999,999') margin
,to_char(sum(sales_adj),'999,999,999') sales_adj ,to_char(sum(sales_adj),'999,999,999') sales_adj
,to_char(sum(sales_adj - scogs_adj),'999,999,999') margin_adj ,to_char(sum(sales_adj - scogs_adj),'999,999,999') margin_adj
,count(iterdef) "# of changes" ,count(DISTINCT logid) "# of changes"
,(max(iterdef->>'stamp'))::timestamptz "last edit" ,COUNT(logid) "# of rows added"
,(max(stamp))::timestamptz "last edit"
FROM FROM
preagg preagg
GROUP BY GROUP BY
GROUPING SETS ( GROUPING SETS (
(director_descr,quota_rep_descr), (director,quota_rep_descr),
(director_descr), (director),
() ()
) )
ORDER BY ORDER BY
director_descr director
,quota_rep_descr ,quota_rep_descr