refactored data processing sql