Combine two queries into one to use in loop

Combine two queries into one to use in loop

error code: 523