2019-04-03 09:56:01 -04:00
|
|
|
WITH
|
|
|
|
preagg AS (
|
|
|
|
SELECT
|
2020-02-19 17:15:43 -05:00
|
|
|
coalesce(director,'None') director
|
|
|
|
,coalesce(quota_rep_descr,'None') quota_rep_descr
|
2019-04-03 09:56:01 -04:00
|
|
|
,part
|
2020-02-19 17:15:43 -05:00
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
2019-04-03 09:56:01 -04:00
|
|
|
,order_month
|
2020-02-19 17:15:43 -05:00
|
|
|
,doc->>'stamp' stamp
|
|
|
|
,logid
|
2020-02-20 14:26:39 -05:00
|
|
|
,tag
|
2021-07-16 09:40:34 -04:00
|
|
|
,sum(value_usd) FILTER (WHERE iter IN ('plan','diff','copy')) sales
|
|
|
|
,sum(cost_usd) FILTER (WHERE iter IN ('plan','diff','copy')) scogs
|
|
|
|
,sum(value_usd) FILTER (WHERE iter <> NOT IN ('plan','diff','copy')) sales_adj
|
|
|
|
,sum(cost_usd) FILTER (WHERE iter <> NOT IN ('plan','diff','copy')) scogs_adj
|
2019-04-03 09:56:01 -04:00
|
|
|
FROM
|
2020-02-19 17:15:43 -05:00
|
|
|
rlarp.osm_pool
|
|
|
|
LEFT OUTER JOIN rlarp.osm_log ON
|
|
|
|
id = logid
|
2019-04-03 09:56:01 -04:00
|
|
|
WHERE
|
2023-04-05 12:37:35 -04:00
|
|
|
order_season = 2024
|
2019-04-03 09:56:01 -04:00
|
|
|
GROUP BY
|
2020-02-19 17:15:43 -05:00
|
|
|
director
|
2019-04-03 09:56:01 -04:00
|
|
|
,quota_rep_descr
|
|
|
|
,part
|
2023-04-05 12:37:35 -04:00
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
2019-04-03 09:56:01 -04:00
|
|
|
,order_month
|
2020-02-19 17:15:43 -05:00
|
|
|
,doc->>'stamp'
|
|
|
|
,logid
|
2020-02-20 14:26:39 -05:00
|
|
|
,tag
|
2019-04-03 09:56:01 -04:00
|
|
|
)
|
|
|
|
SELECT
|
2020-02-19 17:15:43 -05:00
|
|
|
director
|
2019-04-03 09:56:01 -04:00
|
|
|
,quota_rep_descr
|
|
|
|
,to_char(sum(sales),'999,999,999') sales
|
|
|
|
,to_char(sum(sales - scogs),'999,999,999') margin
|
2020-02-20 14:26:39 -05:00
|
|
|
,to_char(sum(sales_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') sales_adj
|
|
|
|
,to_char(sum(sales_adj - scogs_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') margin_adj
|
|
|
|
,count(DISTINCT logid) FILTER (WHERE tag <> 'Initial Build') "# of changes"
|
|
|
|
,COUNT(logid) FILTER (WHERE tag <> 'Initial Build') "# of rows added"
|
|
|
|
,(max(stamp) FILTER (WHERE tag <> 'Initial Build'))::timestamptz "last edit"
|
2019-04-03 09:56:01 -04:00
|
|
|
FROM
|
|
|
|
preagg
|
2023-04-05 12:37:35 -04:00
|
|
|
GROUP BY
|
2019-04-03 09:56:01 -04:00
|
|
|
GROUPING SETS (
|
2020-02-19 17:15:43 -05:00
|
|
|
(director,quota_rep_descr),
|
|
|
|
(director),
|
2019-04-03 09:56:01 -04:00
|
|
|
()
|
|
|
|
)
|
|
|
|
ORDER BY
|
2020-02-19 17:15:43 -05:00
|
|
|
director
|
2021-04-05 15:22:20 -04:00
|
|
|
,quota_rep_descr
|