From b3775210b573759b78349ab6b95e480f294c8a94 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 20 Oct 2017 10:09:55 -0400 Subject: [PATCH] merge pt branch updates --- do_map.pgsql | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/do_map.pgsql b/do_map.pgsql index 5767e93..7c9fff1 100644 --- a/do_map.pgsql +++ b/do_map.pgsql @@ -8,6 +8,7 @@ SELECT m.srce, m.target, t.id, + t.rec, jsonb_build_object( e.v ->> 'key', (t.rec #> ((e.v ->> 'key')::text[])) @@ -30,12 +31,14 @@ FROM LEFT JOIN LATERAL jsonb_array_elements(m.regex->'defn') WITH ORDINALITY e(v, rn) ON true LEFT JOIN LATERAL regexp_matches(t.rec #>> ((e.v ->> 'key')::text[]), e.v ->> 'regex'::text) WITH ORDINALITY mt(mt, rn) ON true WHERE - t.srce = 'PNCC' + t.map IS NULL + AND t.srce = 'PNCC' ORDER BY m.srce, m.seq, m.target, t.id, + t.rec, e.rn ), @@ -47,6 +50,7 @@ agg_rx AS ( rx.srce, rx.target, rx.id, + rx.rec, tps.jsonb_concat_obj(rx.rkey) rkey, tps.jsonb_concat_obj(rx.retval) AS retval, tps.jsonb_concat_obj(CASE rx.retain WHEN 'y' THEN rx.retval ELSE '{}'::jsonb END) retain, @@ -59,27 +63,60 @@ agg_rx AS ( rx.srce, rx.target, rx.id, + rx.rec, rx.seq ) -------------aggregate all targets back to row level (id)------------------------------------------------------------------------------------------------ +,agg_orig AS ( SELECT u.srce, u.id, + u.rec, string_agg(u.target,',') target, - jsonb_pretty(tps.jsonb_concat_obj(coalesce(v.map,'{}'::jsonb) ORDER BY seq )) map, - jsonb_pretty(tps.jsonb_concat_obj(u.retval||coalesce(v.map,'{}'::jsonb) ORDER BY seq)) comb, - jsonb_pretty(tps.jsonb_concat_obj(u.retain||coalesce(v.map,'{}'::jsonb) ORDER BY seq)) retain + tps.jsonb_concat_obj(coalesce(v.map,'{}'::jsonb) ORDER BY seq ) map, + --tps.jsonb_concat_obj(u.retval||coalesce(v.map,'{}'::jsonb) ORDER BY seq) comb, + tps.jsonb_concat_obj(u.retain||coalesce(v.map,'{}'::jsonb) ORDER BY seq) retain FROM --re-aggregate return values and explude any records where one or more regex failed with a null result agg_rx u - LEFT OUTER JOIN tps.map_rv v ON + INNER JOIN tps.map_rv v ON v.target = u.target AND v.srce = u.srce AND v.retval <@ u.retval GROUP BY u.srce, - u.id - LIMIT 1000 \ No newline at end of file + u.id, + u.rec +) + + +--SELECT * FROM agg_orig LIMIT 10 +--UPDATE tps.trans t SET (map) = (SELECT retain FROM agg_orig WHERE t.id = agg_orig.id); + +UPDATE + tps.trans t +SET + map = o.retain +FROM + agg_orig o +WHERE + o.id = t.id + + +/* +SELECT + retain->>'f20', + rec->>'Description', + COUNT(*) +FROM + agg_orig +GROUP BY + retain->>'f20', + rec->>'Description' +ORDER BY + retain->>'f20', + rec->>'Description' +*/ \ No newline at end of file