refactored data analysis sql statements