merge pt branch updates

This commit is contained in:
Paul Trowbridge 2017-10-20 10:09:55 -04:00
parent ccd685ca22
commit b3775210b5

View File

@ -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
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'
*/