Got query working to an extent