SELECT `a`.*, `d`.`acctTypeID`, IFNULL((select debit from comp_opening_balance where companyID = a.companyID and accountID = a.ID and year =2015), 0) as oDebit, IFNULL((select credit from comp_opening_balance where companyID = a.companyID and accountID = a.ID and year =2015), 0) as oCredit, IFNULL((select debit from comp_opening_balance where companyID = a.companyID and accountID = a.ID and year =2015), 0) as oDebitPNL, IFNULL((select credit from comp_opening_balance where companyID = a.companyID and accountID = a.ID and year =2015), 0) as oCreditPNL, IFNULL((select SUM(balance) from view_expence_and_income where companyID = a.companyID and acctType = 3 and year < 2014), 0) as lastRetainInc, IFNULL((select SUM(balance) from view_expence_and_income where companyID = a.companyID and acctType = 4 and year < 2014), 0) as lastRetainExp, IFNULL(sum(b.amountdr), 0) as yeardebit, IFNULL(sum(b.amountcr), 0) as yearcredit, IFNULL((select IFNULL(sum(amountdr), 0) from view_comp_acc_det where companyID = a.companyID and `acctID` = `a`.`ID` and CONCAT(year, if(period<=9, CONCAT("0", period), period)) < 201501 and year >= 2015 GROUP BY acctID), 0) as lastyeardebit, IFNULL((select IFNULL(sum(amountcr), 0) from view_comp_acc_det where companyID = a.companyID and `acctID` = `a`.`ID` and CONCAT(year, if(period<=9, CONCAT("0", period), period)) < 201501 and year >= 2015 GROUP BY acctID), 0) as lastyearcredit
FROM (`comp_chart_of_acct` a)
LEFT OUTER JOIN `view_comp_acc_det` b ON `a`.`ID` = `b`.`acctID` and CONCAT(b.year,if(b.period<=9, CONCAT("0",b.period),b.period)) <= 201512 and CONCAT(b.year,if(b.period<=9, CONCAT("0",b.period),b.period)) >= 201501
LEFT OUTER JOIN `_ref_acct_group` d ON `d`.`ID` = `a`.`acctGroupID`
WHERE `a`.`companyID` =  '3'
AND `a`.`ID` =  '32721'
GROUP BY `a`.`acctCode`
ORDER BY `a`.`acctCode`