refactoring for sql_for_field_with_tags