I have two mysql tables:
table: loan
--------------------------------------------------------
id | advance_id | loan_amount | deposit_amount | date
------------
you can rewrite your query as
SELECT
a.advance_id,
SUM(a.advance_amount) AS adv_amount,
a.purpose,
a.date,
l.loan_amount
FROM
advance AS a
INNER JOIN
(
SELECT advance_id,SUM(deposit_amount) AS loan_amount
FROM loan
GROUP BY advance_id
HAVING SUM(loan_amount) - SUM(deposit_amount) > 0
) AS l USING(advance_id)
GROUP BY a.advance_id
DEMO