Some re-factoring in query in preparation for provider specific SQL gen