From 668ca9f44a8a77de99885123094e973e8967e0c2 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Sun, 15 Oct 2017 23:31:51 -0400 Subject: [PATCH] update data from dcard to reflect to 2 field unique constrain, aggregate several unique constraonts back to 1 row per id --- srce.pgsql | 12 +++++++++--- ubm_data.sql | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/srce.pgsql b/srce.pgsql index 05bb9d2..aa7a730 100644 --- a/srce.pgsql +++ b/srce.pgsql @@ -50,15 +50,21 @@ $$; --this needs to aggregate on id sequence --******************************************* SELECT - jsonb_build_object( + ---creates a key value pair and then aggregates rows of key value pairs + jsonb_object_agg( (ae.e::text[])[1], --the key name (row_to_json(i)::jsonb) #> ae.e::text[] --get the target value from the key from the csv row that has been converted to json ) json_key, srce, - ae.rn, + --ae.rn, id FROM csv_i i INNER JOIN tps.srce s ON s.srce = 'DCARD' - LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS_TEXT(defn->'unique_constraint'->'fields') WITH ORDINALITY ae(e, rn) ON TRUE; \ No newline at end of file + LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS_TEXT(defn->'unique_constraint'->'fields') WITH ORDINALITY ae(e, rn) ON TRUE +GROUP BY + srce, + id +ORDER BY + id \ No newline at end of file diff --git a/ubm_data.sql b/ubm_data.sql index 93538cf..73f3f86 100644 --- a/ubm_data.sql +++ b/ubm_data.sql @@ -30,7 +30,7 @@ SET search_path = tps, pg_catalog; -- INSERT INTO srce (srce, defn) VALUES ('PNCC', '{"name": "PNCC", "type": "csv", "schema": [{"key": "AsOfDate", "type": "date"}, {"key": "BankId", "type": "text"}, {"key": "AccountNumber", "type": "text"}, {"key": "AccountName", "type": "text"}, {"key": "BaiControl", "type": "text"}, {"key": "Currency", "type": "text"}, {"key": "Transaction", "type": "text"}, {"key": "Reference", "type": "text"}, {"key": "Amount", "type": "text"}, {"key": "Description", "type": "text"}, {"key": "AdditionalRemittance", "type": "text"}], "unique_constraint": {"type": "range", "fields": ["{AsOfDate}"]}}'); -INSERT INTO srce (srce, defn) VALUES ('DCARD', '{"name": "DCARD", "type": "csv", "schema": [{"key": "Trans. Date", "type": "date"}, {"key": "Post Date", "type": "text"}, {"key": "Description", "type": "text"}, {"key": "Amount", "type": "text"}, {"key": "Category", "type": "text"}], "unique_constraint": {"type": "key", "fields": ["{Post Date}"]}}'); +INSERT INTO srce (srce, defn) VALUES ('DCARD', '{"name": "DCARD", "type": "csv", "schema": [{"key": "Trans. Date", "type": "date"}, {"key": "Post Date", "type": "date"}, {"key": "Description", "type": "text"}, {"key": "Amount", "type": "text"}, {"key": "Category", "type": "text"}], "unique_constraint": {"type": "key", "fields": ["{Post Date}","{Trans. Date}"]}}'); --