vault backup: 2023-11-03 11:21:44
This commit is contained in:
parent
65c4f689cc
commit
4e641e6dd6
126
sql/get.sql
126
sql/get.sql
@ -1,68 +1,62 @@
|
|||||||
WITH
|
CREATE OR REPLACE FUNCTION rlarp.gethist(_item text, _cust text)
|
||||||
getj AS (
|
RETURNS jsonb
|
||||||
SELECT
|
LANGUAGE plpgsql
|
||||||
(
|
AS $func$
|
||||||
SELECT
|
DECLARE
|
||||||
jsonb_build_object('mold',JSON_AGG(DISTINCT stlc)) doc
|
result jsonb;
|
||||||
FROM
|
BEGIN
|
||||||
"CMS.CUSLG".itemm
|
WITH getj AS (
|
||||||
WHERE
|
SELECT (
|
||||||
item ~ 'TUH10000A10B04'
|
SELECT jsonb_build_object('mold', JSON_AGG(DISTINCT stlc))
|
||||||
) ||
|
FROM "CMS.CUSLG".itemm
|
||||||
(
|
WHERE item ~ _item
|
||||||
SELECT
|
) || (
|
||||||
jsonb_build_object('cust',JSONB_AGG(DISTINCT c.dba))
|
SELECT jsonb_build_object('cust', JSONB_AGG(DISTINCT c.dba))
|
||||||
FROM
|
FROM rlarp.cust c
|
||||||
rlarp.cust c
|
WHERE c.dba ~ _cust
|
||||||
WHERE
|
) AS doc
|
||||||
c.dba ~ 'DIAMOND R'
|
),
|
||||||
) doc
|
agg AS (
|
||||||
)
|
SELECT
|
||||||
,agg AS (
|
gset,
|
||||||
SELECT
|
(SELECT string_agg(ae.v, '.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) AS agglvl,
|
||||||
gset
|
season,
|
||||||
,(SELECT string_agg(ae.v,'.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) agglvl
|
(SELECT doc FROM getj) AS gdoc
|
||||||
,season
|
FROM rlarp.price_pool_dev p
|
||||||
,(select doc from getj) gdoc
|
WHERE
|
||||||
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(
|
gset @> jsonb_build_object(
|
||||||
'cust',(SELECT doc->'cust'->>0 FROM getj)
|
'mold', (SELECT doc->'mold'->>0 FROM getj),
|
||||||
|
'vers', 'A'
|
||||||
)
|
)
|
||||||
OR NOT gset ? 'cust'
|
AND (
|
||||||
)
|
gset @> jsonb_build_object(
|
||||||
ORDER BY
|
'cust', (SELECT doc->'cust'->>0 FROM getj)
|
||||||
agglevel ASC
|
)
|
||||||
)
|
OR NOT gset ? 'cust'
|
||||||
,aggfinal AS (
|
)
|
||||||
SELECT
|
ORDER BY agglevel ASC
|
||||||
jsonb_build_object(
|
),
|
||||||
agg.agglvl,
|
aggfinal AS (
|
||||||
jsonb_agg(gset || jsonb_build_object('season',season))
|
SELECT
|
||||||
) data
|
jsonb_build_object(
|
||||||
,gdoc
|
agg.agglvl,
|
||||||
FROM
|
jsonb_agg(gset || jsonb_build_object('season', season))
|
||||||
agg
|
) AS data,
|
||||||
GROUP BY
|
gdoc
|
||||||
agglvl
|
FROM agg
|
||||||
,gdoc
|
GROUP BY agglvl, gdoc
|
||||||
)
|
),
|
||||||
,final AS (
|
final AS (
|
||||||
SELECT
|
SELECT
|
||||||
public.jsonb_obj_aggc(data) data
|
public.jsonb_obj_aggc(data) AS data,
|
||||||
, gdoc
|
gdoc
|
||||||
FROM
|
FROM aggfinal
|
||||||
aggfinal
|
GROUP BY gdoc
|
||||||
GROUP BY
|
)
|
||||||
gdoc
|
SELECT data INTO result FROM final;
|
||||||
)
|
|
||||||
--SELECT gdoc, jsonb_pretty(data) FROM final
|
RETURN result;
|
||||||
SELECT gdoc, data FROM final
|
|
||||||
|
END
|
||||||
|
$func$;
|
||||||
|
|
||||||
|
68
sql/getdebug.pg.sql
Normal file
68
sql/getdebug.pg.sql
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
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
|
||||||
|
)
|
||||||
|
,aggfinal AS (
|
||||||
|
SELECT
|
||||||
|
jsonb_build_object(
|
||||||
|
agg.agglvl,
|
||||||
|
jsonb_agg(gset || jsonb_build_object('season',season))
|
||||||
|
) data
|
||||||
|
,gdoc
|
||||||
|
FROM
|
||||||
|
agg
|
||||||
|
GROUP BY
|
||||||
|
agglvl
|
||||||
|
,gdoc
|
||||||
|
)
|
||||||
|
,final AS (
|
||||||
|
SELECT
|
||||||
|
public.jsonb_obj_aggc(data) data
|
||||||
|
, gdoc
|
||||||
|
FROM
|
||||||
|
aggfinal
|
||||||
|
GROUP BY
|
||||||
|
gdoc
|
||||||
|
)
|
||||||
|
--SELECT gdoc, jsonb_pretty(data) FROM final
|
||||||
|
SELECT gdoc, data FROM final
|
Loading…
Reference in New Issue
Block a user