1

I'm gettintg crazy with the result of this query

This query returns 29.970 rows (distinct IdDireccionIne):

select distinct IdDireccionIne from DireccionIne as DI

This query returns 29.544 rows (distinct IdDireccionIne):

select distinct IdDireccionIne from DireccionCorregida 
  where IdDireccionIne is not null

I want to look for the IdDireccionIne of the first query (29.970 rows) which are not in the second query (29.544) I should get 29.970 - 29.544 = 426 record.

select distinct IdDireccionIne from DireccionIne as DI  -- 29.970
where IdDireccionIne not in
(select distinct IdDireccionIne from DireccionCorregida 
 where IdDireccionIne is not null) -- 29.544

Surprisingly I get 0 rows!! What is wrong with this query?

EDIT: I've even copied and pasted the results of the two queries in Excel and I found by hand some records of the first result which are not in the second one. I've checked it many times. I'm not worng. What can be wrong with this? IdDireccionIne is an int in both tables.

2nd EDIT: As suggested by one comment, if I do this:

select * from (select distinct IdDireccionIne from DireccionIne as DI) as T1
full outer join
(select distinct IdDireccionIne from DireccionCorregida 
   where IdDireccionIne is not null) as T2
on T1.IdDireccionIne = T2.IdDireccionIne
where T1.IdDireccionIne is null or T2.IdDireccionIne is null

I get the expected 426 rows with values on the left side and nulls on the right side. But I still don't understand why my original query doesn't find them.

4

1 回答 1

4

Can you try the following query:

select distinct IdDireccionIne
from DireccionIne as DI  -- 29.970
where IdDireccionIne not in (select distinct DireccionCorregida.IdDireccionIne
                             from DireccionCorregida
                             where DireccionCorregida.IdDireccionIne is not null)

The scoping rules should clearly take IdDireccionIne from the subquery from clause. But I wonder if there could be some sort of confusion there.

于 2012-06-28T17:58:27.577 回答