Refactoring select statement