tps/database/reports/stacked.sql

87 lines
2.5 KiB
MySQL
Raw Permalink Normal View History

2024-08-29 13:34:50 -04:00
with
dcard AS (
SELECT
2024-08-29 13:47:15 -04:00
'dcard' srce
,r.id id
,r.logid logid
2024-08-29 13:34:50 -04:00
--,r."Trans. Date"
2024-08-29 13:47:15 -04:00
,r."Post Date" pdate
,r."Description" descr
,-r."Amount" amount
,r."Category" category
,r."Party" party
,r."Reason" reason
,-(SUM(r."Amount") OVER (ORDER BY r."Post Date" asc , r."Description") + 1083.50) balance
2024-08-29 13:34:50 -04:00
FROM
tpsv.dcard_mapped r
ORDER BY
r."Post Date" desc
)
,hunt as (
SELECT
'hunt' srce
,r.id
,r.logid
--,r."Reference Number"
--,r."Payee Name"
,r."Date"
,r."Memo"
,r."Amount"
,r."Cateogry Name"
,r."Party"
,r."Reason"
,SUM(r."Amount") OVER (ORDER BY r."Date" asc , r.id) + 29909.75 balance
FROM
tpsv.hunt_mapped r
ORDER BY
r."Date" desc
)
,chase AS (
SELECT
'chase' srce
,r.id
,r.logid
--,r."Trans Date"
,r."Post Date"
,r."Description"
2024-08-29 13:47:15 -04:00
,r."Amount" "Amount"
2024-08-29 13:34:50 -04:00
,r."Type"
,null::text "Party"
,null::text "Reason"
2024-08-29 13:47:15 -04:00
,-(SUM(-r."Amount") OVER (ORDER BY r."Post Date" asc , r.id) + 374.23) balance
2024-08-29 13:34:50 -04:00
FROM
tpsv.chase_default r
ORDER BY
r."Post Date" desc
)
,stacked AS (
2024-08-29 13:47:15 -04:00
SELECT * FROM dcard
UNION ALL
SELECT * FROM hunt
UNION ALL
SELECT * FROM chase
2024-08-29 13:34:50 -04:00
)
2024-09-11 21:42:42 -04:00
,format AS (
2024-08-29 13:47:15 -04:00
SELECT
s.srce
,s.id
,s.logid
,s.pdate
,s.descr
,s.amount
2024-08-29 14:06:05 -04:00
--,s.category
2024-08-29 13:47:15 -04:00
,s.party
,s.reason
2024-08-29 14:06:05 -04:00
--,s.balance
,CASE WHEN s.srce = 'hunt' THEN s.balance ELSE null END hunt
,CASE WHEN s.srce = 'dcard' THEN s.balance ELSE null END dcard
,CASE WHEN s.srce = 'chase' THEN s.balance ELSE null END chase
2024-08-29 13:47:15 -04:00
,sum(s.amount) over (ORDER BY s.pdate asc, s.id) + 28452.02 netbalance
FROM
stacked s
ORDER BY
s.pdate desc
2024-08-29 14:06:05 -04:00
,s.id desc
2024-09-11 21:42:42 -04:00
)
SELECT * FROM format