2016-08-28 2 views
1

das ist meine Frage:Unzusammenhängendes Ergebnis im Ausgang?

SELECT TOP 10000 [Service_order] 
     ,[COMPANY] 
     ,[PENDING_DAYS] 
     ,[SERVICE_TYPE] 
     ,[SERVICE_TYPE_TXT] 
     ,[STATUS] 
     ,[STATUS_TEXT] 
     ,[REASON] 
     ,[REASON_TEXT] 
     ,[ASC code] 
     ,[ASC name] 
     ,[MODEL] 
     ,[INOUTWTY] 
     ,[Part_code1] 
     ,[PS1] 
     ,[confirmation_No1] 
     ,[Part_code2] 
     ,[PS2] 
     ,[SO_NO2] 
     ,[Part_code3] 
     ,[PS3] 
     ,[SO_NO3] 
     ,[Part_code4] 
     ,[PS4] 
     ,[SO_NO4] 
     ,[Part_code5] 
     ,[PS5] 
     ,[SO_NO5] 
     ,[Engineer name] 
    FROM ['NewLP'] 
where REASON_TEXT = 'Parts Not available (ASC)' 
or REASON_TEXT = 'Parts In Transit (Samsung)' 
or REASON_TEXT = 'Parts Back Ordered (Samsung)' 
    and PS1 = 'U' 
    and PS2 = 'U' or PS2 = '' 
    and PS3 = 'U' or PS3 = '' 
    and PS4 = 'U' or PS4 = '' 
    and PS5 = 'U' or PS5 = '' 

Ich weiß nicht, welcher Teil genau ist falsch, aber die reason_text sollte ‚U‘ und der Rest sollte ‚U‘ oder diejenigen, die in dem Zustand und die ersten ps sein sollte sein leer, aber wenn ich die Abfrage ausführen, ist das Ergebnis nicht das, was es sein sollte. hier ist das Ergebnis:

enter image description here

Und wenn ich eines der reason_text in der Bedingung verwenden nur die ersten zwei korrekt Show PS, der Rest (PS3, PS4, PS5) wird im Anschluss an die Bedingung nicht wahr?

Hier ist das Ergebnis, wenn ich einen der reason_Text verwende, wählte ich nur die Spalten, die wichtig sind. enter image description here

Ich würde jede Hilfe zu schätzen wissen.

Antwort

4

Sie benötigen PS* und REASON TEXT setzen, wo mit OR Bedingungen in Klammern wie folgt aus: Denken Sie daran,

where (
     REASON_TEXT = 'Parts Not available (ASC)' 
or REASON_TEXT = 'Parts In Transit (Samsung)' 
or REASON_TEXT = 'Parts Back Ordered (Samsung)' 
    ) 
and PS1 = 'U' 
and (PS2 = 'U' or PS2 = '') 
and (PS3 = 'U' or PS3 = '') 
and (PS4 = 'U' or PS4 = '') 
and (PS5 = 'U' or PS5 = '') 

Der Operator AND hat Vorrang vor OR und wenn diese Bedingungen kombiniert werden, ist es wichtig, Klammern zu verwenden, damit die Datenbank weiß, in welcher Reihenfolge Bewerten Sie jede Bedingung.

Voll Abfrage

SELECT TOP 10000 [Service_order] 
     ,[COMPANY] 
     ,[PENDING_DAYS] 
     ,[SERVICE_TYPE] 
     ,[SERVICE_TYPE_TXT] 
     ,[STATUS] 
     ,[STATUS_TEXT] 
     ,[REASON] 
     ,[REASON_TEXT] 
     ,[ASC code] 
     ,[ASC name] 
     ,[MODEL] 
     ,[INOUTWTY] 
     ,[Part_code1] 
     ,[PS1] 
     ,[confirmation_No1] 
     ,[Part_code2] 
     ,[PS2] 
     ,[SO_NO2] 
     ,[Part_code3] 
     ,[PS3] 
     ,[SO_NO3] 
     ,[Part_code4] 
     ,[PS4] 
     ,[SO_NO4] 
     ,[Part_code5] 
     ,[PS5] 
     ,[SO_NO5] 
     ,[Engineer name] 
    FROM ['NewLP'] 
    where (
     REASON_TEXT = 'Parts Not available (ASC)' 
    or REASON_TEXT = 'Parts In Transit (Samsung)' 
    or REASON_TEXT = 'Parts Back Ordered (Samsung)' 
     ) 
    and PS1 = 'U' 
    and (PS2 = 'U' or PS2 = '') 
    and (PS3 = 'U' or PS3 = '') 
    and (PS4 = 'U' or PS4 = '') 
    and (PS5 = 'U' or PS5 = '') 
+0

Hi, ich habe deinen Code benutzt und das Ergebnis war nichts ?! – erfan

+0

Scheint, dass keine Daten Ihrer __correct__ where-Klausel entsprechen. –

2

Ausführungsreihenfolge in where-Klausel weiß nicht, wie correct.To Kontrolle unter

where (REASON_TEXT = 'Parts Not available (ASC)' 
or REASON_TEXT = 'Parts In Transit (Samsung)' 
or REASON_TEXT = 'Parts Back Ordered (Samsung)') 
    and PS1 = 'U' 
    and (PS2 = 'U' or PS2 = '') 
    and (PS3 = 'U' or PS3 = '') 
    and (PS4 = 'U' or PS4 = '') 
    and (PS5 = 'U' or PS5 = '') 
+0

vereinfacht werden, aber nachdem ich es tat, war nichts im Ergebnisraster! – erfan

+0

Die WHERE-Klausel ist nicht wahr, also keine Ergebnismenge..überprüfen Sie, ob einer der reason_text-Parameter die Bedingung 'Teile *' hat. – TheGameiswar

+0

und auch "ps1 bis 5 hat u oder" – TheGameiswar

1

Versuchen Hinzufügen Klammer wie folgt aus:

... 
    and PS1 = 'U' 
    and (PS2 = 'U' or PS2 = '') 
    and (PS3 = 'U' or PS3 = '') 
    and (PS4 = 'U' or PS4 = '') 
    and (PS5 = 'U' or PS5 = '') 
1

Das Problem ist AND hat höhere Priorität als OR Operator. Sie benötigen also die richtigen Klammern, um die Ausführungsreihenfolge zu steuern.

Ihre Where Klausel kann wie diese

where REASON_TEXT in ('Parts Not available (ASC)', 
         'Parts In Transit (Samsung)', 
         'Parts Back Ordered (Samsung)') 
    and PS1 = 'U' 
    and PS2 in ('U','') 
    and PS3 in ('U','') 
    and PS4 in ('U','') 
    and PS5 in ('U','') 
+0

und (len (Part_code1)> 0 und confirmation_No1 = '') – erfan

+0

und (len (Part_code2)> 0 und SO_NO2 = '') – erfan

+0

hi, sind diese beiden in der richtigen form? – erfan