;WITH Debit(Name,ID,TotalSum) AS
(
select Name,ID,sum(debitValue) from #DR1 GROUP by id,Name
),
Credit(Name,ID,TotalSum) AS
(
select Name,ID,sum(CreditValue) from #CR1 GROUP by id,Name
)
select Debit.Name,Credit.TotalSum as CR,Debit.TotalSum as DR,(Credit.TotalSum-Debit.TotalSum) Balance FROM Debit
inner join Credit on Debit.Id=Credit.ID