Only show last month's overflow

This commit is contained in:
2021-12-08 15:21:10 +00:00
parent 11179a1593
commit 5e18d51b5d
4 changed files with 49 additions and 14 deletions

View File

@ -30,14 +30,27 @@ SELECT categories.id, categories.name, category_groups.name as group,
WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date
), 0)
)::decimal(12,2) as available,
(
COALESCE((
( COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < @from_date
), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date
), 0)
), 0)-CASE WHEN (COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < @prev_from_date
), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < @prev_from_date
), 0)) < 0 THEN (COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < @prev_from_date
), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < @prev_from_date
), 0)) ELSE 0 END
)::decimal(12,2) as available_last_month,
COALESCE((
SELECT SUM(t_this.amount) FROM transactions t_this