SELECT sum(-FAMOUNT) AMT , tr.WACCOUNTID,-tr.WACCOUNTID NEXTID from  account left join Transact tr a on (tr.Waccountid = a.WAccountid and a.WAccountTypeID = 1 and  a.WAccountid = <@WACCOUNTID@>)
where  tr.WTRANSACTIONID not in (
SELECT d.WOPENITEMTRANSACTID from dochead d join 
account a on (d.Waccountid = a.WAccountid and a.WAccountTypeID = 1)
where   d.bposted = 1 and d.WTYPEID in (10)
)
group by tr.WaCCOUNTID 
order by 2