SQL Server-NO ESTÁ


Necesito construir una consulta que me muestre los registros que están en la Tabla 1, pero que no están en la Tabla 2, basado en la combinación make-model-serial number.

Sé de hecho que hay 4 registros que difieren, pero mi consulta siempre vuelve en blanco.

SELECT  *  
FROM Table1 WHERE MAKE+MODEL+[Serial Number] NOT IN
(SELECT make+model+[serial number] FROM Table2)

La tabla 1 tiene 5 registros.

Cuando cambio la consulta a IN, obtengo 1 registro. ¿Qué estoy haciendo mal con el NOT?

Author: SteveC, 2011-04-21

6 answers

Es debido a el camino NO EN obras.

Para evitar estos dolores de cabeza (y para una consulta más rápida en muchos casos), siempre prefiero NO EXISTE:

SELECT  *  
FROM Table1 t1 
WHERE NOT EXISTS (
    SELECT * 
    FROM Table2 t2 
    WHERE t1.MAKE = t2.MAKE
    AND   t1.MODEL = t2.MODEL
    AND   t1.[Serial Number] = t2.[serial number]);
 36
Author: Dave Markle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:53:19

Probablemente sea mejor comparar los campos individualmente, en lugar de concatenar las cadenas.

SELECT t1.*
    FROM Table1 t1
        LEFT JOIN Table2 t2
            ON t1.MAKE = t2.MAKE
                AND t1.MODEL = t2.MODEL
                AND t1.[serial number] = t2.[serial number]
    WHERE t2.MAKE IS NULL
 6
Author: Joe Stefanelli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-21 19:11:48
SELECT [T1].*
FROM [Table1] AS [T1]
WHERE  NOT EXISTS (SELECT 
    1 AS [C1]
    FROM [Table2] AS [T2]
    WHERE ([T2].[MAKE] = [T1].[MAKE]) AND
        ([T2].[MODEL] = [T1].[MODEL]) AND
        ([T2].[Serial Number] = [T1].[Serial Number])
);
 1
Author: Kris Ivanov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-21 19:19:04
SELECT  *  FROM Table1 
WHERE MAKE+MODEL+[Serial Number]  not in
    (select make+model+[serial number] from Table2 
     WHERE make+model+[serial number] IS NOT NULL)

Que funcionó para mí, donde make+model+[serial number] era un nombre de campo

 1
Author: Imtiaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-23 09:51:13

Use una COMBINACIÓN IZQUIERDA comprobando el lado derecho para ver si hay nulos.

SELECT a.Id
FROM TableA a
LEFT JOIN TableB on a.Id = b.Id
WHERE b.Id IS NULL

Lo anterior haría coincidir TableA y TableB en función de la columna Id en cada una, y luego le daría las filas donde el lado B está vacío.

 0
Author: Rebecca Chernoff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-21 19:11:41

Un problema podría ser que si make, model o [serial number] fueran null, los valores nunca se devolverían. Debido a que las concatenaciones de cadenas con valores null siempre dan como resultado null, y not in () con null siempre devolverá nada. El remedio para esto es usar un operador como IsNull(make, ") + IsNull(Model, "), etc.

 0
Author: Shan Plourde,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-21 19:13:04