forecast_api/route_sql/build/rep_totals.sql

48 lines
1.1 KiB
MySQL
Raw Normal View History

2019-04-03 09:56:01 -04:00
WITH
preagg AS (
SELECT
director_descr
,quota_rep_descr
,part
,bill_cust
,ship_cust
,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
FROM
rlarp.osm_fcpool
WHERE
order_season = 2020
GROUP BY
director_descr
,quota_rep_descr
,part
,bill_cust
,ship_cust
,order_month
,iterdef
)
SELECT
director_descr
,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"
2019-04-16 10:18:37 -04:00
,(max(iterdef->>'stamp'))::timestamptz "last edit"
2019-04-03 09:56:01 -04:00
FROM
preagg
GROUP BY
GROUPING SETS (
(director_descr,quota_rep_descr),
(director_descr),
()
)
ORDER BY
director_descr
,quota_rep_descr