2016-05-03 2 views
0

Sie für Ihre Hilfe im Voraus danken, hier gehe ich:Wählen Sie Datensätze mit einer von zwei Möglichkeiten in SQL Server Query

ich eine Tabelle wie folgt aus:

|Content | Syndication_type | Syndication_publication| 
------------------------------------------------------ 
| A |   1  |    1   | 
| A |   2  |    1   | 
| B |   2  |    1   | 
| C |   1  |    0   | 
| D |   1  |    0   | 
| D |   2  |    1   | 
| E |   2  |    1   | 
| F |   1  |    1   | 

Ich brauche Inhalt zu erhalten, die hat nur eine syndication_type mit syndication_publication = 1.

Zum Beispiel, wenn ich syndication_type = 2 wählen, ich habe Content = B, erhalten Content = D und Content = E, da sie nur syndication_publication = 1 für syndication_type = 2 haben.

Content = 2 ist nicht der Fall, da es sowohl Syndication_type = 1 und Syndication_type = 2 mit Syndication_publication = 1 hat, während Content = D ok ist, da es nur Syndication_publication = 2 mit Syndication_publication = 1 hat.

Ich hoffe, dass ich mein Ziel ... erklärt :)

Vielen Dank für Ihre Hilfe.

+0

Bitte zeigen Sie uns Ihre Anfrage so weit. –

Antwort

0

kann nicht verwendet werden VORHANDEN

SELECT [Content] 
FROM MyTable mt 
WHERE [Syndication_type] = 2 
     AND NOT EXISTS (
      SELECT 1 
      FROM MyTable mt2 
      WHERE mt2.[Content] = mt.[Content] 
        AND mt2.Syndication_publication = 1 
        AND mt2.Syndication_type <> mt.Syndication_type 
     ) 
+0

Vielen Dank, die Lösung funktioniert, wenn ich [Syndication_publication = 1] nach dem ersten schließen WHERE [Syndication_type] = 2 :) – Donna

1

Versuchen:

SELECT Content 
FROM yourtable 
WHERE Syndication_type = 1 -- your conditions 
GROUP BY Content, Syndication_type 
HAVING COUNT(DISTINCT Syndication_publication) = 1 
+0

Sorry, ich habe versucht, aber auch Content = A rausgekommen ... und es ist nicht korrekt, da A sowohl syndication_type = 1 als auch syndication_type = 2 mit syndication_publication = 1 hat, daher muss A aus dem Ergebnis ausgeschlossen werden . Danke – Donna

Verwandte Themen