updates in progress

This commit is contained in:
Paul Trowbridge 2017-10-24 17:37:20 -04:00
parent 57fa123adf
commit 937cf38af1
6 changed files with 73 additions and 8 deletions

View File

@ -1,4 +1,4 @@
\timing
SELECT
r.*,
SUM(r."Advances"+r."Adjustments"-r."Payments") OVER (ORDER BY r."Post Date" asc ,r."Reference #" asc)

View File

@ -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
$$;
*/

17
srce_defn.pgsql Normal file
View File

@ -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

16
trans_log_template.pgsql Normal file
View File

@ -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
)

View File

@ -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);
--

View File

@ -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: -
--