refactor mutliple SQL entities into one giant SQL statement