merge query function