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