refactor reports sql