2019-03-12 17:20:23 -04:00
|
|
|
--\timing
|
|
|
|
--explain (analyze, buffers)
|
2023-02-27 16:15:35 -05:00
|
|
|
WITH
|
2019-03-12 17:20:23 -04:00
|
|
|
mseq AS (
|
2023-02-27 16:15:35 -05:00
|
|
|
SELECT * FROM
|
2019-03-12 17:20:23 -04:00
|
|
|
(
|
|
|
|
VALUES
|
2020-02-13 01:11:51 -05:00
|
|
|
('01 - Jun',1)
|
|
|
|
,('02 - Jul',2)
|
|
|
|
,('03 - Aug',3)
|
|
|
|
,('04 - Sep',4)
|
|
|
|
,('05 - Oct',5)
|
|
|
|
,('06 - Nov',6)
|
|
|
|
,('07 - Dec',7)
|
|
|
|
,('08 - Jan',8)
|
|
|
|
,('09 - Feb',9)
|
|
|
|
,('10 - Mar',10)
|
|
|
|
,('11 - Apr',11)
|
|
|
|
,('12 - May',12)
|
2019-03-12 17:20:23 -04:00
|
|
|
|
|
|
|
) x(m,s)
|
|
|
|
)
|
|
|
|
--select * from mseq
|
|
|
|
,base AS (
|
|
|
|
SELECT
|
2019-03-18 17:27:01 -04:00
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
2019-03-12 17:20:23 -04:00
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
,SUM(units) units
|
|
|
|
,SUM(value_usd) value_usd
|
|
|
|
FROM
|
2020-02-13 01:11:51 -05:00
|
|
|
rlarp.osm_pool
|
2019-03-12 17:20:23 -04:00
|
|
|
WHERE
|
2019-03-12 17:32:28 -04:00
|
|
|
where_clause
|
2023-03-07 17:32:45 -05:00
|
|
|
--quota_rep_descr = 'MATTHEW STAAL'
|
|
|
|
AND order_season IN (2019,2024)
|
2019-03-12 17:20:23 -04:00
|
|
|
GROUP BY
|
2019-03-18 17:27:01 -04:00
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
2019-03-12 17:20:23 -04:00
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
)
|
2019-03-19 00:22:00 -04:00
|
|
|
, basket1 AS (
|
2019-03-18 17:27:01 -04:00
|
|
|
SELECT
|
|
|
|
part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
2019-03-19 00:22:00 -04:00
|
|
|
,SUM(value_usd) value_usd
|
2019-03-18 17:27:01 -04:00
|
|
|
FROM
|
|
|
|
base
|
|
|
|
GROUP BY
|
|
|
|
part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
2019-03-19 00:22:00 -04:00
|
|
|
)
|
|
|
|
,basket AS (
|
|
|
|
SELECT
|
|
|
|
part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
2019-03-21 16:50:51 -04:00
|
|
|
,CASE WHEN sum(value_usd) over () = 0 THEN 0 ELSE value_usd/sum(value_usd) over () END mix
|
2019-03-19 00:22:00 -04:00
|
|
|
FROM
|
|
|
|
basket1
|
2023-02-27 16:15:35 -05:00
|
|
|
ORDER BY
|
2019-03-19 00:24:57 -04:00
|
|
|
mix DESC
|
2019-03-18 17:27:01 -04:00
|
|
|
)
|
2019-03-12 17:20:23 -04:00
|
|
|
,months AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,order_month
|
|
|
|
,mseq.s seq
|
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
base
|
|
|
|
INNER JOIN mseq ON
|
|
|
|
mseq.m = base.order_month
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,order_month
|
|
|
|
,s
|
|
|
|
)
|
|
|
|
,mpvt AS (
|
|
|
|
SELECT
|
|
|
|
order_month
|
|
|
|
,seq
|
2023-03-07 17:32:45 -05:00
|
|
|
,SUM(units) FILTER (WHERE order_season = 2019) "2023 qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2024) "2024 tot qty"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2019) "2023 value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2024) "2024 tot value_usd"
|
2019-03-12 17:20:23 -04:00
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_month
|
|
|
|
,seq
|
|
|
|
ORDER BY
|
|
|
|
seq ASC
|
|
|
|
)
|
|
|
|
,mlist AS (
|
|
|
|
SELECT
|
|
|
|
mseq.m order_month
|
2023-03-01 17:07:44 -05:00
|
|
|
,"2023 qty"
|
|
|
|
,"2024 base qty"
|
|
|
|
,"2024 adj qty"
|
|
|
|
,"2024 tot qty"
|
|
|
|
,"2023 value_usd"
|
|
|
|
,"2024 base value_usd"
|
|
|
|
,"2024 adj value_usd"
|
|
|
|
,"2024 tot value_usd"
|
2019-03-12 17:20:23 -04:00
|
|
|
FROM
|
|
|
|
mseq
|
|
|
|
LEFT OUTER JOIN mpvt ON
|
|
|
|
mpvt.order_month = mseq.m
|
|
|
|
ORDER BY
|
|
|
|
mseq.s ASC
|
|
|
|
)
|
|
|
|
,totals AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
)
|
2020-02-18 14:25:57 -05:00
|
|
|
,tags AS (
|
|
|
|
SELECT DISTINCT
|
|
|
|
doc->>'tag' tag
|
|
|
|
FROM
|
|
|
|
rlarp.osm_log
|
2023-02-27 16:15:35 -05:00
|
|
|
WHERE
|
2020-02-25 12:03:30 -05:00
|
|
|
doc ? 'tag'
|
|
|
|
AND id <> 1
|
2020-02-18 14:25:57 -05:00
|
|
|
UNION
|
|
|
|
SELECT * FROM
|
2023-02-27 16:15:35 -05:00
|
|
|
(VALUES
|
2020-02-18 14:25:57 -05:00
|
|
|
('price'),
|
|
|
|
('volume')
|
|
|
|
) x(tag)
|
|
|
|
)
|
2020-03-13 10:48:26 -04:00
|
|
|
,custs AS (
|
|
|
|
SELECT
|
|
|
|
bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
,SUM(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
base
|
|
|
|
GROUP BY
|
|
|
|
bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
)
|
2023-02-27 16:15:35 -05:00
|
|
|
SELECT
|
2019-03-12 17:20:23 -04:00
|
|
|
jsonb_build_object(
|
|
|
|
'months'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
|
|
|
|
,'mpvt'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
|
2019-03-18 17:27:01 -04:00
|
|
|
,'basket'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(basket)::jsonb) FROM basket)
|
2019-03-12 17:20:23 -04:00
|
|
|
,'totals'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
|
2020-02-18 14:25:57 -05:00
|
|
|
,'tags'
|
|
|
|
,(SELECT jsonb_agg(tag) FROM tags)
|
2020-03-13 10:48:26 -04:00
|
|
|
,'customers'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(custs)::jsonb) FROM custs)
|
2021-04-05 12:36:51 -04:00
|
|
|
) package
|