Fix to build query