int,
[Name] varchar(20)
)
insert into test0 values (1,'first'),(2,'second'),(3,'third'),(4,'fourth'),(5,'fifth')
insert into test1 values (1,12,3),(1,51,4),(1,17,16),(2,31,55),(2,77,9),(2,54,87),
(3,21,3),(3,15,23),(3,22,19),(4,46,35),(4,56,18),(4,82,11),
(5,10,3),(5,51,44),(5,17,63) When
query: Select a.Code,a.Name,(Select sum(Debit)-Sum(Credit) from Test1 where Code = a.Code) Balance
from Test0 a Its result is: 
What you want to query may be like this: Select b.* from (
Select a.Code newCode,a.Name newName,(Select sum(Debit)-Sum(Credit) from Test1 where Code = a.Code) Balance
from Test0 a) b
where b.Balance > 0 Its
result is: 
It can be clearly seen that it only queries records with Balance greater than 0. Hope this can help you. Best regards, Xudong Peng |