Refactor into one sql