Rewritten query now works