price_api/sql/get.sql

62 lines
1.4 KiB
MySQL
Raw Normal View History

2023-11-03 09:35:57 -04:00
WITH
2023-11-03 11:00:54 -04:00
getj AS (
SELECT
(
SELECT
jsonb_build_object('mold',JSON_AGG(DISTINCT stlc)) doc
FROM
"CMS.CUSLG".itemm
WHERE
item ~ 'TUH10000A10B04'
) ||
(
SELECT
jsonb_build_object('cust',JSONB_AGG(DISTINCT c.dba))
FROM
rlarp.cust c
WHERE
c.dba ~ 'DIAMOND R'
) doc
)
,agg AS (
2023-11-03 09:35:57 -04:00
SELECT
gset
2023-11-03 11:00:54 -04:00
,(SELECT string_agg(ae.v,'.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) agglvl
2023-11-03 09:35:57 -04:00
,season
2023-11-03 11:00:54 -04:00
,(select doc from getj) gdoc
2023-11-03 09:35:57 -04:00
FROM
2023-11-03 11:00:54 -04:00
rlarp.price_pool_dev p
2023-11-03 09:35:57 -04:00
WHERE
2023-11-03 11:00:54 -04:00
--gut the exact mold and actuals only
2023-11-03 09:35:57 -04:00
gset @> jsonb_build_object(
2023-11-03 11:00:54 -04:00
'mold',(SELECT doc->'mold'->>0 FROM getj),
2023-11-03 09:35:57 -04:00
'vers','A'
)
2023-11-03 11:00:54 -04:00
--pull either the exact customer or no customer
2023-11-03 09:35:57 -04:00
AND (
2023-11-03 11:00:54 -04:00
gset @> jsonb_build_object(
'cust',(SELECT doc->'cust'->>0 FROM getj)
)
2023-11-03 09:35:57 -04:00
OR NOT gset ? 'cust'
)
ORDER BY
agglevel ASC
)
2023-11-03 11:00:54 -04:00
,final AS (
SELECT
jsonb_build_object(
agg.agglvl,
jsonb_build_object(
'data',
jsonb_agg(gset || jsonb_build_object('season',season))
)
) data
,gdoc
FROM
agg
GROUP BY
agglvl
,gdoc
)
select * from final