From e0c5cf47a5f05ba02807120a4a907e4ce8e391f6 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 27 Oct 2017 01:44:41 -0400 Subject: [PATCH] build interpretation of evt.log for running totals --- evt_log_gl_extract.pgsql | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/evt_log_gl_extract.pgsql b/evt_log_gl_extract.pgsql index d172e08..32a25f3 100644 --- a/evt_log_gl_extract.pgsql +++ b/evt_log_gl_extract.pgsql @@ -1,22 +1,34 @@ -WITH j AS ( -SELECT - id, - post_stmp, - rec as r -FROM - evt.log -) - --this is a dynamic approach that dumps all keys into the json except several that are required which it extracts +WITH +expand_gl AS ( SELECT id, ARRAY['GL',rn::text] json_path, post_stmp, - a.i->>'amt' amount, + (a.i->>'amt')::numeric amount, a.i->>'account' account, - a.i->>'date' tran_date, - a.i - '{amt,account,date}'::text[] as therest + j.rec->'header'->>'date' tran_date, + j.rec->'header'->>'vendor' vendor, + (a.i - '{amt,account,date}'::text[])||j.rec->'header' as therest FROM - j - LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(j.r->'GL') WITH ORDINALITY a(i, rn) ON TRUE \ No newline at end of file + evt.log j + LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(j.rec->'GL') WITH ORDINALITY a(i, rn) ON TRUE +) +,gl_agg AS ( +SELECT + id + , tran_date + , vendor + , SUM(amount) amt + , ROUND(SUM(amount) FILTER (WHERE account = 'dcard'),2) dr +FROM + expand_gl +GROUP BY + id + , tran_date + , vendor +ORDER BY + id asc +) +SELECT id, tran_date, vendor, amt, dr, sum(dr) over(ORDER BY id) FROM gl_agg \ No newline at end of file