price_api/sql/gethist.sql

66 lines
1.7 KiB
MySQL
Raw Normal View History

2023-11-03 11:21:44 -04:00
CREATE OR REPLACE FUNCTION rlarp.gethist(_item text, _cust text)
RETURNS jsonb
LANGUAGE plpgsql
AS $func$
DECLARE
result jsonb;
BEGIN
WITH getj AS (
2023-11-03 14:30:43 -04:00
SELECT jsonb_build_object('mold',_item,'cust',_cust) doc
--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
2023-11-03 11:21:44 -04:00
),
agg AS (
SELECT
gset,
(SELECT string_agg(ae.v, '.') FROM jsonb_array_elements_text(p.agglevel) ae(v)) AS agglvl,
season,
2023-11-10 01:44:37 -05:00
stats,
2023-11-03 11:21:44 -04:00
(SELECT doc FROM getj) AS gdoc
2023-11-10 01:44:37 -05:00
FROM
rlarp.price_pool_dev p
2023-11-03 11:21:44 -04:00
WHERE
2023-11-03 11:00:54 -04:00
gset @> jsonb_build_object(
2023-11-03 14:30:43 -04:00
'mold', (SELECT doc->>'mold' FROM getj),
2023-11-03 11:21:44 -04:00
'vers', 'A'
2023-11-03 11:00:54 -04:00
)
2023-11-03 11:21:44 -04:00
AND (
gset @> jsonb_build_object(
2023-11-03 14:30:43 -04:00
'cust', (SELECT doc->>'cust' FROM getj)
2023-11-03 11:21:44 -04:00
)
OR NOT gset ? 'cust'
)
ORDER BY agglevel ASC
),
aggfinal AS (
SELECT
jsonb_build_object(
agg.agglvl,
2023-11-10 01:44:37 -05:00
jsonb_agg(gset || jsonb_build_object('season', season,'stats',stats))
2023-11-03 11:21:44 -04:00
) 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$;