2013-05-28 17 views
7

Desidero ottenere l'ID della tabella [interazioni] ma questi ID non devono essere uguali alla tabella [EmailOUT]. Non potrei scrivere la domanda.tSQL NOT IN Query

Select ID from EmailOut   
where ID NOT IN   
    (select ID from 
    [172.28.101.120].[GenesysIS].dbo.interactions 
    where media_type = 'email' 
    and type = 'Outbound') 

qualcosa di simile a questo. Desidero email in uscita nella tabella delle interazioni, ma queste email potrebbero esistere nella tabella EmailOut. Voglio rimuoverli. conteggio in uscita e-mail a circa 300 ma questo risultato query dovrebbe meno di 300

+0

Sono i tuoi chiavi esterne e primarie nome esattamente come accennato? Interactions.ID vs EmailOut.ID? – madC

risposta

14

Sembra si dovrebbe invertire la tua ricerca, se si vuole ottenere l'ID del di [interazioni] tabella:

select ID from 
[172.28.101.120].[GenesysIS].dbo.interactions 
where media_type = 'email' 
and type = 'Outbound' 
AND ID NOT IN (SELECT ID FROM EmailOut) 
+0

sì, questo è quello che voglio. Grazie. restituisce 157 righe. – cihata87

+0

.. e puoi anche usare NOT IN senza selezionare, come questo esempio snip dove controlli i numeri interi: non in (119, 138, 158, 165) Solo per menzionare un modo per testare l'operatore – netfed

-1

Che dire

select ID from [172.28.101.120].[GenesysIS].dbo.interactions 
where media_type = 'email' 
and type = 'Outbound' 
minus 
select ID from EmailOut 
+0

cosa è meno? fornisce sintassi errata vicino all'errore 'minus' – cihata87

1

provare questo -

SELECT t2.* 
FROM [172.28.101.120].[GenesysIS].dbo.interactions t2 
WHERE t2.media_type = 'email' 
    AND t2.[type] = 'Outbound' 
    AND NOT EXISTS (
      SELECT 1 
      FROM dbo.EmailOut t 
      WHERE t.id = t2.id 
     ) 
+0

no non risolve – cihata87

+0

Si prega di provare la risposta aggiornata. – Devart