build interpretation of evt.log for running totals

This commit is contained in:
Paul Trowbridge 2017-10-27 01:44:41 -04:00
parent a5b48325a2
commit e0c5cf47a5

View File

@ -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 --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 SELECT
id, id,
ARRAY['GL',rn::text] json_path, ARRAY['GL',rn::text] json_path,
post_stmp, post_stmp,
a.i->>'amt' amount, (a.i->>'amt')::numeric amount,
a.i->>'account' account, a.i->>'account' account,
a.i->>'date' tran_date, j.rec->'header'->>'date' tran_date,
a.i - '{amt,account,date}'::text[] as therest j.rec->'header'->>'vendor' vendor,
(a.i - '{amt,account,date}'::text[])||j.rec->'header' as therest
FROM FROM
j evt.log j
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(j.r->'GL') WITH ORDINALITY a(i, rn) ON TRUE 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