SELECT `b`.*, `a`.`effective_date`, IFNULL((SELECT DISTINCT referenceNo from view_journal_table where formNo = b.formNo and companyID = b.companyID LIMIT 0, 1), "---") as refNo, CONCAT((SELECT sam2.acctName FROM comp_journal_detail sam1
 JOIN comp_chart_of_acct sam2 on sam2.ID = sam1.acctCode
 WHERE sam1.companyID = b.companyID 
 and sam1.journalID = b.journalID 
 and sam1.sequence = IF(b.sequence%2 = 0, b.sequence-1, b.sequence+1)), ' - ', SUBSTR(a.description, 1, 20)) AS description
FROM (`comp_journal` a)
JOIN `comp_journal_detail` b ON `b`.`journalID` = `a`.`journalID` and b.companyID = a.companyID
WHERE `a`.`companyID` =  '3'
AND CONCAT(a.year,LPAD(a.period,2,"0")) <= '201601'
AND CONCAT(a.year,LPAD(a.period,2,"0")) >= '201601'
AND `a`.`status` =  1
ORDER BY `a`.`effective_date`, `a`.`journalID`, `sequence`