with report title cost as select tag title transactions outcome from t

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
with report (title, cost)
as
(
select tag.title, transactions.outcome
from transactions, tag
where transactions.tag = tag.id and tag.parent is null and transactions.deleted = 0
union all
select parent.title, transactions.outcome
from tag as parent
left join tag as child on parent.id = child.parent
join transactions on child.id = transactions.tag
where transactions.deleted = 0
)
select title , sum(cost) as total
from report
group by title
having total <> 0