This is possible, but is complex.
Sql custom where put the value into DB query, and mysql support IF statement, so for example:
( IF( {user avatar displayed raw} IN (406,407), 1, 0) = 1 AND group_id IN (12,8) ) OR ( IF( {user avatar displayed raw} IN (408,409), 1, 0) = 1 AND group_id IN (12) ) OR ( IF( {user avatar displayed raw} IN (410,411), 1, 0) = 1 AND group_id IN (8) )
in this example:
- if you see profile page of user 406 and 407 you will see a list of users from group 12 and 8
- if you see profile page of user 408 and 409 you will see a list of users from group 12
- if you see profile page of user 410 and 411 you will see a list of users from group 8
{user avatar displayed raw} this code return id of current displayed user