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