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='2018-02-28 23:59:59' AND cp.var='avgt' order by thresh ASC' at line 1 in rank_query line 1194