Fixed bug in sql statement