=30 and days < 60, owing, 0.00)) as 30days,
sum(if(days >=60 and days < 90, owing, 0.00)) as 60days,
sum(if(days >=90,owing,0.00)) as 90days,
sum(owing) as balance
from
(select e.entity_id as id, e.name as name,
if(f.credit,-(f.total-f.allocated_amount),(f.total-f.allocated_amount)) as owing,
datediff(curdate(), a.activity_start_time) as days
from participations p
join acts a on a.act_id = p.act_id
join entities e on e.entity_id = p.entity_id
join financial_acts f on f.financial_act_id = a.act_id
where p.arch_short_name = "participation.customerAccountBalance"
) as accountbalances
group by id
order by name]]>