2019-03-01 17:39:44 -05:00
|
|
|
\timing
|
|
|
|
--explain (analyze, buffers)
|
2019-03-02 00:30:29 -05:00
|
|
|
WITH
|
|
|
|
mseq AS (
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
VALUES
|
|
|
|
('Jun',1)
|
|
|
|
,('Jul',2)
|
|
|
|
,('Aug',3)
|
|
|
|
,('Sep',4)
|
|
|
|
,('Oct',5)
|
|
|
|
,('Nov',6)
|
|
|
|
,('Dec',7)
|
|
|
|
,('Jan',8)
|
|
|
|
,('Feb',9)
|
|
|
|
,('Mar',10)
|
|
|
|
,('Apr',11)
|
|
|
|
,('May',12)
|
|
|
|
|
|
|
|
) x(m,s)
|
|
|
|
)
|
|
|
|
--select * from mseq
|
|
|
|
,base AS (
|
2019-03-01 17:39:44 -05:00
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
,SUM(units) units
|
|
|
|
,SUM(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
rlarp.osm_fcpool
|
|
|
|
WHERE
|
|
|
|
quota_rep_descr = '90005 - MARK WILKINSON'
|
|
|
|
AND segm = 'Retail'
|
|
|
|
AND mod_chan = 'MASS'
|
2019-03-02 00:30:29 -05:00
|
|
|
--AND order_month = 'May'
|
2019-03-01 17:39:44 -05:00
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
)
|
|
|
|
,months AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,order_month
|
2019-03-02 00:30:29 -05:00
|
|
|
,mseq.s seq
|
2019-03-01 17:39:44 -05:00
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
base
|
2019-03-02 00:30:29 -05:00
|
|
|
INNER JOIN mseq ON
|
|
|
|
mseq.m = base.order_month
|
2019-03-01 17:39:44 -05:00
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,order_month
|
2019-03-02 00:30:29 -05:00
|
|
|
,s
|
|
|
|
)
|
|
|
|
,mpvt AS (
|
|
|
|
SELECT
|
|
|
|
order_month
|
|
|
|
,seq
|
|
|
|
,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 = 'adjustment') "2020 adj 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 = 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 IN ('copy','adjustment')) "2020 tot value_usd"
|
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_month
|
|
|
|
,seq
|
|
|
|
ORDER BY
|
|
|
|
seq ASC
|
2019-03-01 17:39:44 -05:00
|
|
|
)
|
|
|
|
,totals AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
jsonb_build_object(
|
|
|
|
'months'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
|
2019-03-02 00:30:29 -05:00
|
|
|
'mpvt'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(mpvt)::jsonb) FROM mpvt)
|
2019-03-01 17:39:44 -05:00
|
|
|
,'base'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
|
|
|
|
,'totals'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
|
|
|
|
) package
|