add sql files

This commit is contained in:
Paul Trowbridge 2019-03-12 17:20:23 -04:00
parent 91d6b11204
commit 1ecadc3227
2 changed files with 208 additions and 208 deletions

View File

@ -1,80 +1,80 @@
WITH rows AS ( WITH rows AS (
SELECT SELECT
---------customer info----------------- ---------customer info-----------------
bill_cust_descr bill_cust_descr
,billto_group ,billto_group
,ship_cust_descr ,ship_cust_descr
,shipto_group ,shipto_group
,quota_rep_descr ,quota_rep_descr
,director_descr ,director_descr
,segm ,segm
,mod_chan ,mod_chan
,mod_chansub ,mod_chansub
---------product info------------------ ---------product info------------------
,majg_descr ,majg_descr
,ming_descr ,ming_descr
,majs_descr ,majs_descr
,mins_descr ,mins_descr
,brand ,brand
,part_family ,part_family
,part_group ,part_group
,branding ,branding
,color ,color
,part_descr ,part_descr
---------dates------------------------- ---------dates-------------------------
,order_season ,order_season
,order_month ,order_month
,ship_season ,ship_season
,ship_month ,ship_month
,request_season ,request_season
,request_month ,request_month
,promo ,promo
,version ,version
,iter ,iter
--------values------------------------- --------values-------------------------
,sum(value_loc) value_loc ,sum(value_loc) value_loc
,sum(value_usd) value_usd ,sum(value_usd) value_usd
,sum(cost_loc) cost_loc ,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd ,sum(cost_usd) cost_usd
,sum(units) units ,sum(units) units
FROM FROM
rlarp.osm_fcpool rlarp.osm_fcpool
WHERE WHERE
quota_rep_descr = 'rep_replace' quota_rep_descr = 'rep_replace'
GROUP BY GROUP BY
---------customer info----------------- ---------customer info-----------------
bill_cust_descr bill_cust_descr
,billto_group ,billto_group
,ship_cust_descr ,ship_cust_descr
,shipto_group ,shipto_group
,quota_rep_descr ,quota_rep_descr
,director_descr ,director_descr
,segm ,segm
,mod_chan ,mod_chan
,mod_chansub ,mod_chansub
---------product info------------------ ---------product info------------------
,majg_descr ,majg_descr
,ming_descr ,ming_descr
,majs_descr ,majs_descr
,mins_descr ,mins_descr
,brand ,brand
,part_family ,part_family
,part_group ,part_group
,branding ,branding
,color ,color
,part_descr ,part_descr
---------dates------------------------- ---------dates-------------------------
,order_season ,order_season
,order_month ,order_month
,ship_season ,ship_season
,ship_month ,ship_month
,request_season ,request_season
,request_month ,request_month
,promo ,promo
,version ,version
,iter ,iter
) )
SELECT SELECT
json_agg(row_to_json(rows)) x json_agg(row_to_json(rows)) x
FROM FROM
rows rows

View File

@ -1,130 +1,130 @@
--\timing --\timing
--explain (analyze, buffers) --explain (analyze, buffers)
WITH WITH
mseq AS ( mseq AS (
SELECT * FROM SELECT * FROM
( (
VALUES VALUES
('Jun',1) ('Jun',1)
,('Jul',2) ,('Jul',2)
,('Aug',3) ,('Aug',3)
,('Sep',4) ,('Sep',4)
,('Oct',5) ,('Oct',5)
,('Nov',6) ,('Nov',6)
,('Dec',7) ,('Dec',7)
,('Jan',8) ,('Jan',8)
,('Feb',9) ,('Feb',9)
,('Mar',10) ,('Mar',10)
,('Apr',11) ,('Apr',11)
,('May',12) ,('May',12)
) x(m,s) ) x(m,s)
) )
--select * from mseq --select * from mseq
,base AS ( ,base AS (
SELECT SELECT
order_season order_season
,order_month ,order_month
,version ,version
,iter ,iter
,part_descr ,part_descr
,bill_cust_descr ,bill_cust_descr
,ship_cust_descr ,ship_cust_descr
,SUM(units) units ,SUM(units) units
,SUM(value_usd) value_usd ,SUM(value_usd) value_usd
FROM FROM
rlarp.osm_fcpool rlarp.osm_fcpool
WHERE WHERE
'where_clause' 'where_clause'
GROUP BY GROUP BY
order_season order_season
,order_month ,order_month
,version ,version
,iter ,iter
,part_descr ,part_descr
,bill_cust_descr ,bill_cust_descr
,ship_cust_descr ,ship_cust_descr
) )
,months AS ( ,months AS (
SELECT SELECT
order_season order_season
,version ,version
,iter ,iter
,order_month ,order_month
,mseq.s seq ,mseq.s seq
,sum(units) units ,sum(units) units
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
base base
INNER JOIN mseq ON INNER JOIN mseq ON
mseq.m = base.order_month mseq.m = base.order_month
GROUP BY GROUP BY
order_season order_season
,version ,version
,iter ,iter
,order_month ,order_month
,s ,s
) )
,mpvt AS ( ,mpvt AS (
SELECT SELECT
order_month order_month
,seq ,seq
,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty" ,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty"
,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty" ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty"
,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj qty" ,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj qty"
,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot qty" ,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot qty"
,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot value_usd"
FROM FROM
months months
GROUP BY GROUP BY
order_month order_month
,seq ,seq
ORDER BY ORDER BY
seq ASC seq ASC
) )
,mlist AS ( ,mlist AS (
SELECT SELECT
mseq.m order_month mseq.m order_month
,"2019 qty" ,"2019 qty"
,"2020 base qty" ,"2020 base qty"
,"2020 adj qty" ,"2020 adj qty"
,"2020 tot qty" ,"2020 tot qty"
,"2019 value_usd" ,"2019 value_usd"
,"2020 base value_usd" ,"2020 base value_usd"
,"2020 adj value_usd" ,"2020 adj value_usd"
,"2020 tot value_usd" ,"2020 tot value_usd"
FROM FROM
mseq mseq
LEFT OUTER JOIN mpvt ON LEFT OUTER JOIN mpvt ON
mpvt.order_month = mseq.m mpvt.order_month = mseq.m
ORDER BY ORDER BY
mseq.s ASC mseq.s ASC
) )
,totals AS ( ,totals AS (
SELECT SELECT
order_season order_season
,version ,version
,iter ,iter
,sum(units) units ,sum(units) units
,sum(value_usd) value_usd ,sum(value_usd) value_usd
FROM FROM
months months
GROUP BY GROUP BY
order_season order_season
,version ,version
,iter ,iter
) )
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'months' 'months'
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months) ,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
,'mpvt' ,'mpvt'
,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist) ,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
,'base' ,'base'
,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base) ,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
,'totals' ,'totals'
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals) ,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
) package ) package