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 ') AND cp.enddate='2017-04-19 23:59:59' AND cp.var='avgt' order by thresh ASC' at line 1 in rank_query line 1194