Fix sql query. thanks to dethegeek. closes #2457