From 937cf38af15915ba4d18ec3cf54fd04ee48aa268 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 24 Oct 2017 17:37:20 -0400 Subject: [PATCH] updates in progress --- loan_bal.pgsql | 2 +- srce.pgsql | 20 ++++++++++++++------ srce_defn.pgsql | 17 +++++++++++++++++ trans_log_template.pgsql | 16 ++++++++++++++++ ubm_data.sql | 2 +- ubm_schema.sql | 24 ++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 srce_defn.pgsql create mode 100644 trans_log_template.pgsql diff --git a/loan_bal.pgsql b/loan_bal.pgsql index 240c296..60da152 100644 --- a/loan_bal.pgsql +++ b/loan_bal.pgsql @@ -1,4 +1,4 @@ - +\timing SELECT r.*, SUM(r."Advances"+r."Adjustments"-r."Payments") OVER (ORDER BY r."Post Date" asc ,r."Reference #" asc) diff --git a/srce.pgsql b/srce.pgsql index f749a77..6e7e67c 100644 --- a/srce.pgsql +++ b/srce.pgsql @@ -1,3 +1,6 @@ +SET auto_explain.log_min_duration = 0; +SHOW ALL; + \timing /*-------------------------------------------------------- @@ -59,6 +62,12 @@ BEGIN EXECUTE _t; + + +END +$$; + + WITH -------------for each imported row in the COPY table, genereate the json rec, and a column for the json key specified in the srce.defn----------- @@ -77,7 +86,7 @@ BEGIN FROM csv_i i INNER JOIN tps.srce s ON - s.srce = _srce + s.srce = 'PNCO' LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS_TEXT(defn->'unique_constraint'->'fields') WITH ORDINALITY ae(e, rn) ON TRUE GROUP BY i.*, @@ -155,12 +164,13 @@ BEGIN --------insert to log------------------------------------------------------------------------------------------------------------------------------------- --below select should be loaded to the log table - + --------summarize records not inserted-------------------+------------------------------------------------------------------------------------------------ + SELECT - t.srce + jsonb_build_object('srce', t.srce) srce, ,(ae.e::text[])[1] unq_constr ,MIN(rec #>> ae.e::text[]) min_text ,MAX(rec #>> ae.e::text[]) max_text @@ -173,6 +183,4 @@ BEGIN GROUP BY t.srce ,(ae.e::text[])[1]; - -END -$$; \ No newline at end of file + */ \ No newline at end of file diff --git a/srce_defn.pgsql b/srce_defn.pgsql new file mode 100644 index 0000000..79046fb --- /dev/null +++ b/srce_defn.pgsql @@ -0,0 +1,17 @@ +WITH +ext AS ( +SELECT + srce + ,defn->'unique_constraint'->>'fields' + ,ARRAY(SELECT ae.e::text[] FROM jsonb_array_elements_text(defn->'unique_constraint'->'fields') ae(e)) txa +FROM + tps.srce +) +SELECT + srce + , + public.jsonb_extract(rec,txa) +FROM + tps.trans + INNER JOIN ext ON + trans.srce = ext.srce \ No newline at end of file diff --git a/trans_log_template.pgsql b/trans_log_template.pgsql new file mode 100644 index 0000000..07b958e --- /dev/null +++ b/trans_log_template.pgsql @@ -0,0 +1,16 @@ +SELECT +jsonb_pretty( +$$ +{ + "path":"C:\\users\\ptrowbridge\\downloads\\transsearchcsv.csv" + ,"srce":"PNCC" + ,"stamp":"2017-10-24 08:32:06.599067-04" + ,"inserted":{ + "keys":[ + 1,2,3,4,5,6,7 + ] + ,"summary":"" + } +} +$$::jsonb +) \ No newline at end of file diff --git a/ubm_data.sql b/ubm_data.sql index 238684a..a83a65c 100644 --- a/ubm_data.sql +++ b/ubm_data.sql @@ -168,7 +168,7 @@ SET search_path = tps, pg_catalog; -- Name: trans_id_seq; Type: SEQUENCE SET; Schema: tps; Owner: - -- -SELECT pg_catalog.setval('trans_id_seq', 1544069, true); +SELECT pg_catalog.setval('trans_id_seq', 1544080, true); -- diff --git a/ubm_schema.sql b/ubm_schema.sql index a81bac7..543484f 100644 --- a/ubm_schema.sql +++ b/ubm_schema.sql @@ -119,6 +119,30 @@ CREATE TYPE srce_defn_schema AS ( ); +SET search_path = public, pg_catalog; + +-- +-- Name: jsonb_extract(jsonb, text[]); Type: FUNCTION; Schema: public; Owner: - +-- + +CREATE FUNCTION jsonb_extract(rec jsonb, key_list text[]) RETURNS jsonb + LANGUAGE plpgsql + AS $$ +DECLARE + t text; + j jsonb := '{}'::jsonb; + +BEGIN + FOREACH t IN ARRAY key_list LOOP + j := j || jsonb_build_object(t,rec->t); + END LOOP; + RETURN j; +END; +$$; + + +SET search_path = tps, pg_catalog; + -- -- Name: jsonb_concat(jsonb, jsonb); Type: FUNCTION; Schema: tps; Owner: - --