SELECT AccNum, max(date), Type, sum(Amount) FROM transaction GROUP BY AccNum, Type union SELECT AccNum, max(date),'Balance', sum(if (type='Debit',-1*Amount,Amount)) FROM transaction GROUP BY AccNum; SELECT * FROM transaction t where chequeno is not null or ddno is not null;