Case when exists in where clause sql. Then filter it in the outer query.