bug 1008: Fixed typo and change sorting order in SQL statement