You can try using case statements in the WHERE clause, something like the instructions below. Please note: I do not think that this will be especially optimal for performance.
Performing this action allows you to save it in a single expression:
select userReturnval, userregisterid, OtherValue FROM ( (SELECT otherValue FROM... ) as tblO --unrelated table , ( SELECT userReturnval, userregisterid FROM ( SELECT userReturnval, userregisterid, Rank() OVER (PARTITION BY .. ORDER BY ...) as RANK FROM ... WHERE case --Choose which where clause to use when (select count(userregisterid) from table1 where site =@siteID and userid=@userID ) >0 then case when then 1 else 0 end else case when then 1 else 0 end end = 1 ) as tblRank WHERE (RANK =1) ) as tblR
source share