SELECT DISTINCT cacc.ID, openB.ID as openingBalanceID, concat(substr(cacc.acctCode, 1, 4), "-", substr(cacc.acctCode, 5, 3), "-", substr(cacc.acctCode, 8, 2)) as acctCode, cacc.acctName, accGrp.acctGroupName, accType.acctTypeName, openB.debit as openingBalanceDr, openB.credit as openingBalanceCr
FROM (`comp_chart_of_acct` as cacc)
LEFT JOIN `_ref_acct_group` as accGrp ON `accGrp`.`ID` = `cacc`.`acctGroupID`
LEFT JOIN `comp_acct_type` as accType ON `accType`.`ID` = `accGrp`.`acctTypeID`
LEFT JOIN `comp_opening_balance` as openB ON `cacc`.`ID` = `openB`.`accountID` AND openB.companyID = 3 AND openB.year = 2015
WHERE `accType`.`acctClass` =  1
AND `cacc`.`companyID` =  '3'
ORDER BY `cacc`.`acctCode` asc