0

Total number of records in table i1450:

enter image description here

Total number with condition where i.BROJ is equal to field REFERENCA in other table:

enter image description here

Shouldn't it return difference between last two results (which is 64) when I use NOT IN in WHERE clause?

enter image description here

Both of columns are of varchar type.

3 Answers3

1

If you have any NULL values in the REFERENCA column from the FpsPmtOrderRQ table then the NOT IN clause will not work as expected - (the reason why)

A solution is to remove NULL values from the result returned by the subselect.

SELECT COUNT(*)
FROM i1450 j
WHERE i.BROJ NOT IN (SELECT REFERENCA FROM FpsPmtOrderRQ WHERE REFERENCA IS NOT NULL)
Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107
  • Will it count those records in 1st table in `BROJ` where value is `NULL`? –  Dec 15 '17 at 14:48
0

If the sub-query returns a null value, the IN will not be true. Do NOT EXISTS instead.

select count(*)
from i1450 i
where not exists (select 1 from FpsPmtOrderRQ f
                  where i.broj = f.REFERENCA)
jarlh
  • 42,561
  • 8
  • 45
  • 63
0

I think you need to coalesce your field to handle nulls. That is probably why you get 0. By doing:

where coalesce(I.BROJ,'n/a') not in (select coalesce(REFERENCA,'')

or something similar, you would exclude nulls, and return a proper count.

Dominic
  • 194
  • 4