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 ')R1 LEFT JOIN (select hospcode,sum(target)tg,sum(result)rs,sum(result)*100/s' at line 4