make Query reuseable

fixes #1545
refactored scalar query functions to share common code