Fix bug in query