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