2016-06-01 8 views
-2

Effizient Art und Weise zu schreiben, die unter abfragtprüfen Null/Nicht Null in Spalten

where col_1 is not null and((col_2 is not null and col_3 is not null)<br/> 
              or (col_4 is not null and Col_5 is not null)<br/> 
              or(col_3 is not null and col_4 is not null)<br/> 
              or(col_2 is not null and Col_5 is not null)<br/> 
              )<br/> 

<br/> 
<br/> 
<br/> 
col_1 is null and ((col_2 is null and col_3 is null and col_4 is null and col_5 is null)<br/> 
    or (col_2 is not null and col_3 is null and col_4 is null and col_5 is null)<br/> 
    or (col_2 is null and col_3 is not null and col_4 is null and col_5 is null)<br/> 
    or (col_2 is null and col_3 is null and col_4 is not null and col_5 is null)<br/> 
    or (col_2 is null and col_3 is null and col_4 is null and col_5 is not null)<br/> 
    )<br/> 
+2

Sie sollten wahrscheinlich die Tabellenentwurf ändern. Brauchst du Hilfe dabei? –

+0

ich habe nicht bekommen, was Sie beabsichtigten, das Tischdesign zu ändern? – user3255656

+0

Wenn Sie nur nach mindestens einem nicht null suchen, verwenden Sie: 'coalesce (col1, col2, col3, col4, col5) ist nicht null '. Uns ist nicht klar, nach welchen Kombinationen Sie suchen. – shawnt00

Antwort

0

Karte einfach den Zustand 0 und 1, so können Sie die Anzahl der Spiele aufaddieren:

case when col1 is not null then 1 else 0 end + 
case when col2 is not null then 1 else 0 end + 
case when col3 is not null then 1 else 0 end + 
case when col4 is not null then 1 else 0 end + 
case when col5 is not null then 1 else 0 end = 2 

Ähnliches ...

case when col1 is null then 1 else 0 end + 
case when col2 is null then 1 else 0 end + 
case when col3 is null then 1 else 0 end + 
case when col4 is null then 1 else 0 end + 
case when col5 is null then 1 else 0 end >= 3 
0

Das ist die Frage nach der Logik in der diskreten Mathematik. Für den ersten Teil, können Sie einfach zu

Where col1 is not null 
AND (
     (col3 is not null OR col5 is not null) 
     AND 
     (col4 is not null OR col2 is not null) 
    ) 
Verwandte Themen