From 4e641e6dd65163ff4b9461968ab480114f5f4318 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 3 Nov 2023 11:21:44 -0400 Subject: [PATCH] vault backup: 2023-11-03 11:21:44 --- sql/get.sql | 126 +++++++++++++++++++++----------------------- sql/getdebug.pg.sql | 68 ++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 66 deletions(-) create mode 100644 sql/getdebug.pg.sql diff --git a/sql/get.sql b/sql/get.sql index 7d52102..4808dc6 100644 --- a/sql/get.sql +++ b/sql/get.sql @@ -1,68 +1,62 @@ -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 ( +CREATE OR REPLACE FUNCTION rlarp.gethist(_item text, _cust text) +RETURNS jsonb +LANGUAGE plpgsql +AS $func$ +DECLARE + result jsonb; +BEGIN + WITH getj AS ( + SELECT ( + 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)) + FROM rlarp.cust c + WHERE c.dba ~ _cust + ) AS doc + ), + agg AS ( + SELECT + gset, + (SELECT string_agg(ae.v, '.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) AS agglvl, + season, + (SELECT doc FROM getj) AS gdoc + FROM rlarp.price_pool_dev p + WHERE gset @> jsonb_build_object( - 'cust',(SELECT doc->'cust'->>0 FROM getj) + 'mold', (SELECT doc->'mold'->>0 FROM getj), + 'vers', 'A' ) - 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 + 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)) + ) AS data, + gdoc + FROM agg + GROUP BY agglvl, gdoc + ), + final AS ( + SELECT + public.jsonb_obj_aggc(data) AS data, + gdoc + FROM aggfinal + GROUP BY gdoc + ) + SELECT data INTO result FROM final; + + RETURN result; + +END +$func$; + diff --git a/sql/getdebug.pg.sql b/sql/getdebug.pg.sql new file mode 100644 index 0000000..7d52102 --- /dev/null +++ b/sql/getdebug.pg.sql @@ -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