price_api/sql/gethist_table.pg.sql

46 lines
1.1 KiB
SQL

WITH
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 (
SELECT
gset
,(SELECT string_agg(ae.v,'.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) agglvl
,season
--,(select doc from getj) gdoc
FROM
rlarp.price_pool_dev p
WHERE
--gut the exact mold and actuals only
gset @> jsonb_build_object(
'mold',(SELECT doc->'mold'->>0 FROM getj),
'vers','A'
)
--pull either the exact customer or no customer
AND (
gset @> jsonb_build_object(
'cust',(SELECT doc->'cust'->>0 FROM getj)
)
OR NOT gset ? 'cust'
)
ORDER BY
agglevel ASC
)
SELECT * FROM agg