0

Based on a filter I would like to apply that to what to filter.

For example if I have a variable called p_filter that could have the values 'A' or 'B' I would like to filter specific where clauses based on that.

SELECT *
FROM t1
WHERE
id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ) --only apply this filter if p_filter = 'A'
AND id NOT IN (select id from t3 WHERE t1.id = t3.id(+) )  --only apply this filter if p_filter = 'B'

If the variable though is null, I would like for it to apply NO filters. I tried something like this but it does not work:

SELECT *
FROM t1
WHERE
CASE WHEN :p_filter != 'A' THEN 1 WHEN id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ) THEN 1 ELSE 0 END = 1
AND CASE WHEN :p_filter != 'B' THEN 1 WHEN  id NOT IN (select id from t3 WHERE t1.id = t3.id(+) ) THEN 1 ELSE 0 END = 1;

UPDATE: I am so stupid, I meant if null apply no filters!

3 Answers 3

1

Just do:

WHERE
   --only apply this filter if p_filter = 'A'
   p_filter = 'A' 
   AND
   id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ) 

   OR

   --only apply this filter if p_filter = 'B'
   p_filter = 'B' 
   AND
   id NOT IN (select id from t3 WHERE t1.id = t3.id(+) )  

   OR

   -- If the variable though is null, I would like for it to apply both filters
   p_filter IS NULL
   AND
   id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ) 
   AND
   id NOT IN (select id from t3 WHERE t1.id = t3.id(+) ) 


---- EDIT ---------

I am so sorry, I am stupid, I meant to say if null apply NO filters! – user2924127

In that case just remove the last condition, and add OR p_filter IS NULL:

WHERE
   --only apply this filter if p_filter = 'A'
   p_filter = 'A' 
   AND
   id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ) 

   OR

   --only apply this filter if p_filter = 'B'
   p_filter = 'B' 
   AND
   id NOT IN (select id from t3 WHERE t1.id = t3.id(+) )  

   -- I am so sorry, I am stupid, I meant to say if null apply NO filters!
   OR
   p_filter IS NULL
Sign up to request clarification or add additional context in comments.

1 Comment

I am so sorry, I am stupid, I meant to say if null apply NO filters!
1

To do this "OR" the negative of your condition like so:

SELECT *
FROM t1
WHERE 
(:p_filter IS NULL OR :p_filter!='A' OR id NOT IN (select id from t2 WHERE t1.id = t2.id(+) ))
AND (:p_filter IS NULL OR :p_filter!='B' OR id NOT IN (select id from t3 WHERE t1.id = t3.id(+) ))  

2 Comments

I am so sorry, I am stupid, I meant to say if null apply NO filters!
I adjusted the query above to make null do neither filter.
0
SELECT *
  FROM t1
 WHERE ( 
         (p_filter = 'A' and id NOT IN (select id from t2 WHERE t1.id = t2.id(+) )) 
          or 
         (p_filter = 'B' and id NOT IN (select id from t3 WHERE t1.id = t3.id(+) ))
          or 
         (p_filter is null)
       )

1 Comment

Please add some explanation to your answer. Code-only answers are discouraged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.