refactor create complex query string to remove redundancy