forecast_api/route_meta/scenario_totals.sql

72 lines
1.4 KiB
MySQL
Raw Normal View History

2019-03-01 15:31:31 -05:00
--\timing
--explain (analyze, buffers)
WITH base AS (
2019-02-28 01:48:42 -05:00
SELECT
order_season
2019-03-01 15:31:31 -05:00
,order_month
2019-02-28 01:48:42 -05:00
,version
,iter
2019-03-01 15:31:31 -05:00
,part_descr
,bill_cust_descr
,ship_cust_descr
2019-02-28 01:48:42 -05:00
,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'
AND order_month = 'May'
GROUP BY
order_season
2019-03-01 15:31:31 -05:00
,order_month
2019-02-28 01:48:42 -05:00
,version
2019-03-01 15:31:31 -05:00
,iter
,part_descr
,bill_cust_descr
,ship_cust_descr
)
,months AS (
SELECT
order_season
,version
,iter
,order_month
,sum(units) units
,sum(value_usd) value_usd
FROM
base
GROUP BY
order_season
,version
,iter
,order_month
)
,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)
)
,jsonb_build_object(
'base'
,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
)
,jsonb_build_object(
'totals'
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
)