I cannot use column alias in CASE clause in the following example:
SELECT
((SELECT SUM(t2.amount) FROM trans t2 WHERE u.mail = t2.paid_to)-
(SELECT SUM(t2.amount) FROM trans t2 WHERE u.mail = t2.paid_by)) AS "balance",
(CASE WHEN balance < u.credit_limit THEN 'YES' ELSE 'NO' END) AS "result"
FROM user u
LEFT JOIN trans t ON u.mail = t.paid_to OR u.mail = t.paid_by
How can I solve this problem?
Another question, should I use a different alias for inner selects (t2) or can I also use the same alias (t) as the outer select for trans table?