Time : 12:06:18 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by PARENTINVOICE) inv2 on inv2.PARENTINVOICE=inv1.INVOICEDETAILID where i' at line 3 Raw SQL : select inv1.INVOICEDETAILID,inv1.REFERENCENO,inv1.adjustmentamount,inv2.adjustamt,(inv1.adjustmentamount-inv2.adjustamt) as diffamount, inv1.PARENTINVOICE,if(inv1.DRCR=1,'Cr','Dr') as DRCR,DATE_FORMAT(inv1.ADJUSTMENTDATE,'%d %b %Y') as refwithdate,inv2.TRANSACTIONID from invoicedetail as inv1 left outer join (select sum(adjustmentamount) as adjustamt,INVOICEDETAILID,PARENTINVOICE,TRANSACTIONID,REFERENCENO from invoicedetail where LEDGER= group by PARENTINVOICE) inv2 on inv2.PARENTINVOICE=inv1.INVOICEDETAILID where inv1.LEDGER= group by inv1.REFERENCENO order by inv1.ADJUSTMENTDATE