0

I could not make sense of a query, I found a workaround to get the data but I would like to know what happened.

so basically I want to obtain all ids that are not contained in another table. if I count them separately I get this:

select count(distinct product_sid) 
from gaps_inp.pim_product_channel pc 
where pc.channel_code = 'Amazon' 

count

200658

and then count items on the other table:

select count(w.sid)
from gaps_fend.product_whitelist w 
where w.channel_code  = 'Amazon'

count

39697

but now if I try to count the difference:

select count(*) 
from gaps_inp.pim_product_channel pc 
where pc.channel_code = 'Amazon' 
and pc.product_sid not in (
  select w.sid
  from gaps_fend.product_whitelist w 
  where w.channel_code  = 'Amazon'
);

count

0

both fields gaps_inp.pim_product_channel.product_sid and gaps_fend.product_whitelist.sid are bigint

I was able to do it by using a left join and a where sid is null, but still I would like to know what I did wrong in the where not in query.

this is the workaround:

select count(distinct pc.product_sid)
from gaps_inp.pim_product_channel pc 
left join gaps_fend.product_whitelist w on w.channel_code = 'Amazon' and pc.product_sid = w.sid
where pc.channel_code = 'Amazon'
and w.sid is null;

count

160968

2 Answers 2

2

Am sure there are some NULL values present in below query

select w.sid
from gaps_fend.product_whitelist w 
where w.channel_code  = 'Amazon'

NOT IN fails when the sub-query returns any NULL values so you are getting zero count. So the work around is using LEFT JOIN or NOT EXISTS or adding IS NOT NULL condition in sub-query

NOT EXISTS method which can handle NULL values

SELECT Count(*)
FROM   gaps_inp.pim_product_channel pc
WHERE  pc.channel_code = 'Amazon'
       AND NOT EXISTS (SELECT 1
                       FROM   gaps_fend.product_whitelist w
                       WHERE  w.channel_code = 'Amazon'
                              AND w.sid = pc.product_sid); 
Sign up to request clarification or add additional context in comments.

1 Comment

I missed your answer when I answered.
0

not in returns no rows if there is a NULL value returned by the sub-query.
Add and w.sid is not null

Comments

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.