I need to select from all users that are not present in a set of other sub queries:
SELECT user_id
FROM (<userSubQuery1>)
WHERE
user_id NOT IN (<badUserSubQueryA>) AND
user_id NOT IN (<badUserSubQueryB>) AND
user_id NOT IN (<badUserSubQueryC>)
Only I need to do the NOT IN filters in many different queries where the userSubQuery and the badUserSubQueries may be different. E.g.:
SELECT user_id
FROM (<userSubQuery2>)
WHERE
user_id NOT IN (<badUserSubQueryB>) AND
user_id NOT IN (<badUserSubQueryC>) AND
user_id NOT IN (<badUserSubQueryD>)
All the sub queries, both the ones I'm selecting from and the once used in the NOT IN are complex so I don't want to duplicate the code for the NOT IN sub queries which are often the same (badUserSubQueryB and badUserSubQueryC in my example).
I could achieve this with dynamic sql but I'd rather not if I can avoid it. Is it possible?