Fix buildSQL for charts