2016-12-28 3 views
1
auf einen Wert anwenden

ich die folgende Tabelle haben:MySQL ‚where Fall‘ nur

id | date_inserted | type  | route_id | route_type | km 
1 2016-01-05  Transport null  Normal  null 
2 2016-01-06  Transport null  Normal  50 
3 2016-01-07  Transport 1   Normal  null 
4 2016-04-02  Transport 2   Normal  null 
5 2016-05-03  Services  null  Normal  20 
6 2016-06-21  Transport null  Exceptional 35 

Und ich brauchte die gesamten Strecken nach Monaten abzurufen. Das war erreichen, indem Sie:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 

Der Ausgang ist so etwas wie:

type  | total | month 
Transport 3  1 
Transport 1  2 
Services  1  5 
Transport 1  6 

Und es funktioniert gut. Doch jetzt wurde ich gebeten, einige Bedingungen nur anzuwenden, wenn der Typ Transport ist, die Bedingungen sind die folgenden:

  1. Die route_id darf nicht null sein OR
  2. Die route_type muss Exceptional OR
  3. Die route_type sein Normal ist UND km ist nicht gleich Null

So, nach diesen Bedingungen ist es das, was ich versucht habe:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') AND 
     (CASE WHEN type = 'Transport' THEN 
      route_id IS NOT NULL OR 
      route_type = 'Exceptional' OR 
      (route_type = 'Normal' AND km != 0) 
     END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 

Aber meine Ausgabe lautet:

type  | total | month 
Transport 2  1 
Transport 1  2 
Transport 1  6 

Die Services Reihe fehlt.

+1

eine 'ELSE hinzufügen 'Bedingung für die' CASE'-Anweisung, die True zurückgibt ... z 'CASE WHEN <...> ELSE 1 = 1 END' – abigperson

+0

Ihr Fall wird null für' Services' zurückgeben. Handle es in einem anderen 'When' oder' else' – GurV

+0

@PJSantoro Warum posten Sie dies als Antwort? Die Analyse und die vorgeschlagene Lösung sind beide korrekt. – jpw

Antwort

1

Sie müssen ein else-Segment hinzufügen, um die Dienste zu verarbeiten. Wenn Sie den Fall, dass ja ermöglichen, für die normale Verarbeitung Verwendung 1 = 1.

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
    type IN ('Transport', 'Services') AND 
    (CASE WHEN type = 'Transport' THEN 
     route_id IS NOT NULL OR 
     route_type = 'Exceptional' OR 
     (route_type = 'Normal' AND km != 0) 
    ELSE 
    1 = 1 
    END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC 
1

Wie andere sagt, benötigen Sie einen ELSE, um Ihren Fall hinzuzufügen, andernfalls, wenn die Zeile ‚Service‘ ist, wird es null zurück und zeigt den Datensatz nicht an.

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     type IN ('Transport', 'Services') AND 
     (CASE WHEN type = 'Transport' THEN 
      route_id IS NOT NULL OR 
      route_type = 'Exceptional' OR 
      (route_type = 'Normal' AND km != 0) 
      ELSE 1 = 1 
     END) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC; 

Eine andere Version, die Sie verwenden können (was ich bevorzugen) ist:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     (type = 'Services' OR 
      (type = 'Transport' and 
        (route_id IS NOT NULL OR 
         route_type = 'Exceptional' OR 
         (route_type = 'Normal' AND km != 0) 
        ) 
     ) 
    ) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC; 

Wenn route_type nur Normal und sein Exceptional Sie können die Bedingungen ändern:

SELECT type, COUNT(id) AS `total`, MONTH(date_inserted) AS `month` 
FROM routes 
WHERE YEAR(date_inserted) = '2016' AND 
     (type = 'Services' OR 
      (type = 'Transport' and 
        (route_id IS NOT NULL OR 
        route_type = 'Exceptional' OR 
        km != 0 
        ) 
     ) 
    ) 
GROUP BY type, `month` 
ORDER BY date_inserted ASC, `total` DESC;