Subject RE: [firebird-support] Differents results using EXISTS
Author Leyne, Sean
Eduardo,

> The following query:
>
> SELECT COUNT(distinct(codpostal))
> FROM CUBOLABO WHERE CODPOSTAL <> 0
> AND codpostal not in ( SELECT distinct(codpostal) FROM DETCP WHERE
> intcabzonas = 2)
...
> Returns 1360 (the correct value)
>
>
> If I change to:
>
> SELECT COUNT(distinct(codpostal))
> FROM CUBOLABO WHERE CODPOSTAL <> 0
> AND NOT EXISTS( SELECT distinct(codpostal) FROM DETCP WHERE
intcabzonas =
> 2)
>
...
>
> But returns 0.


The 2 SQL are not equivalent!


The correct NOT EXISTS SQL would be:

SELECT
COUNT(distinct(C.codpostal))
FROM CUBOLABO C
WHERE
C.CODPOSTAL <> 0
AND NOT EXISTS(
SELECT 1
FROM DETCP D
WHERE
(D.intcabzonas = 2)
AND (D.codpostal = C.codpostal)
)


Sean