From 2e6fc0e30a297d3d0540e252933baef53f101f55 Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Thu, 21 Mar 2019 15:13:27 -0400 Subject: [PATCH] insert and return aggregate --- route_sql/new_part.sql | 76 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/route_sql/new_part.sql b/route_sql/new_part.sql index 290382c..a659b00 100644 --- a/route_sql/new_part.sql +++ b/route_sql/new_part.sql @@ -564,6 +564,7 @@ FROM LEFT OUTER JOIN gld sd ON make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag BETWEEN sd.sdat AND sd.edat ) +,final AS ( SELECT COALESCE(m.avdplt,p.awdplt) plnt --master data ,b."ddord#" @@ -734,4 +735,77 @@ FROM cx.perd = '1910' AND cx.rtyp = 'MA' AND cx.fcur = copr.curr - AND cx.tcur = 'US' \ No newline at end of file + AND cx.tcur = 'US' +) +,ins AS ( + INSERT INTO rlarp.osm_fcpool SELECT * FROM final RETURNING * +) +,insagg AS ( +SELECT + bill_cust_descr + ,billto_group + ,ship_cust_descr + ,shipto_group + ,quota_rep_descr + ,director_descr + ,segm + ,mod_chan + ,mod_chansub + ,majg_descr + ,ming_descr + ,majs_descr + ,mins_descr + ,brand + ,part_family + ,part_group + ,branding + ,color + ,part_descr + ,order_season + ,order_month + ,ship_season + ,ship_month + ,request_season + ,request_month + ,promo + ,version + ,iter + ,sum(value_loc) value_loc + ,sum(value_usd) value_usd + ,sum(cost_loc) cost_loc + ,sum(cost_usd) cost_usd + ,sum(units) units +FROM + ins +GROUP BY + bill_cust_descr + ,billto_group + ,ship_cust_descr + ,shipto_group + ,quota_rep_descr + ,director_descr + ,segm + ,mod_chan + ,mod_chansub + ,majg_descr + ,ming_descr + ,majs_descr + ,mins_descr + ,brand + ,part_family + ,part_group + ,branding + ,color + ,part_descr + ,order_season + ,order_month + ,ship_season + ,ship_month + ,request_season + ,request_month + ,promo + ,version + ,iter +) +--SELECT * FROM insagg +SELECT json_agg(row_to_json(insagg)) x from insagg