Below is a complete overview of the problem. The isnull function is also included to provide zero (rather than zero) balance returns for non-transactional tenants.
create table tblTenant ( ID int identity(1,1) primary key not null, Name varchar(100) ); create table tblTransaction ( ID int identity(1,1) primary key not null, tblTenantID int, AmountPaid money, AmountCharged money ); insert into tblTenant(Name) select 'bob' union all select 'Jane' union all select 'john'; insert into tblTransaction(tblTenantID,AmountPaid, AmountCharged) select 1,5.00,10.00 union all select 1,10.00,10.00 union all select 1,10.00,10.00 union all select 2,10.00,15.00 union all select 2,15.00,15.00 select * from tblTenant select * from tblTransaction SELECT tenant.ID, tenant.Name, isnull(SUM(Trans.AmountPaid) - SUM(Trans.AmountCharged),0) AS Balance FROM tblTenant tenant LEFT JOIN tblTransaction Trans ON tenant.ID = Trans.tblTenantID GROUP BY tenant.ID, tenant.Name; drop table tblTenant; drop table tblTransaction;
source share