SQLTeam.com | Weblogs | Forums

MSSQL Current balance check

We have given a task as -

Written query as

SET NOCOUNT ON;


select 
  concat(u.first_name, ' ',  u.last_name) name, 
  u.opa, 
  b.balance + u.credit_limit current_balance,
  case
    when b.balance + u.credit_limit >= 0 then 'NO'
    else 'YES'
  end credit_limit_breached  
from customer u
inner join (
  select vpa, sum(total_amount) balance
  from (
    select paid_by vpa, sum(-amount) total_amount
    from transaction_log
    group by paid_by
     union all
    select paid_to vpa, sum(amount) total_amount
    from transaction_log
    group by paid_to
  ) bi
  group by vpa
) b on b.vpa = u.opa;

go

Not getting desire result. Does our joining condition is f9?