diff --git a/build/rep_totals.sql b/build/rep_totals.sql index dde3617..7e7defd 100644 --- a/build/rep_totals.sql +++ b/build/rep_totals.sql @@ -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 - ,quota_rep_descr + director + ,quota_rep_descr \ No newline at end of file