none
CASE Variable in WHERE nutzen RRS feed

  • Frage

  • Hallo zusammen,

    ich habe folgendes Problem. 

    Dieser Query funktioniert:

    --------------------------------------------

    SELECT 
    ResourceFunctionAllocation.DateStart AS datum,
    ResourceFunctionAllocation.DateEnd AS datumEND,

    CASE WHEN UPPER(ResourceAllocation.Custom1) = 'IN' THEN ResourceFunctionAllocation.DateEnd
    WHEN UPPER(ResourceAllocation.Custom1) = 'OUT' THEN ResourceFunctionAllocation.DateStart
    ELSE '2077-01-01 23:00:00'
    END AS datumSORTIERUNG

    FROM ResourceFunctionAllocation 

    JOIN ResourceAllocation
    On (ResourceFunctionAllocation.IdResourceFunctionAllocation = ResourceAllocation.IdResourceFunctionAllocation)

    where 
    DateStart >= '2017-10-16T00:00:00.000' AND 
    DateStart <= '2017-10-16T23:59:59.999'
    ORDER BY datumSORTIERUNG

    --------------------------------------------

    Nun möchte ich jedoch das Feld "datumSORTIERUNG" in der Where-Clause nutzen, also wie folgt:

    --------------------------------------------

    SELECT 
    ResourceFunctionAllocation.DateStart AS datum,
    ResourceFunctionAllocation.DateEnd AS datumEND,

    CASE WHEN UPPER(ResourceAllocation.Custom1) = 'IN' THEN ResourceFunctionAllocation.DateEnd
    WHEN UPPER(ResourceAllocation.Custom1) = 'OUT' THEN ResourceFunctionAllocation.DateStart
    ELSE '2077-01-01 23:00:00'
    END AS datumSORTIERUNG

    FROM ResourceFunctionAllocation 

    JOIN ResourceAllocation
    On (ResourceFunctionAllocation.IdResourceFunctionAllocation = ResourceAllocation.IdResourceFunctionAllocation)

    where 
    datumSORTIERUNG >= '2017-10-16T00:00:00.000' AND 
    datumSORTIERUNG <= '2017-10-16T23:59:59.999'
    ORDER BY datumSORTIERUNG

    --------------------------------------------

    ... und da kommt die Fehlermeldung:


    "Meldung 207, Ebene 16, Status 1, Zeile 16
    Ungültiger Spaltenname 'datumSORTIERUNG'.
    Meldung 207, Ebene 16, Status 1, Zeile 17
    Ungültiger Spaltenname 'datumSORTIERUNG'."

    Nun habe ich herausgefunden, dass man eine CASE-Variable nicht in der WHERE-Clause nutzen kann, aber wie kann ich meine Abfrage denn nun realisieren?

    1000 Dank


    Montag, 16. Oktober 2017 12:32

Antworten

  • Hallo ,
    versuche es einfach erneut zu schachteln:

    select x.*
    from
    (
    SELECT
    ResourceFunctionAllocation.DateStart AS datum,
    ResourceFunctionAllocation.DateEnd AS datumEND,
    CASE WHEN UPPER(ResourceAllocation.Custom1) = 'IN' THEN ResourceFunctionAllocation.DateEnd
    WHEN UPPER(ResourceAllocation.Custom1) = 'OUT' THEN ResourceFunctionAllocation.DateStart
    ELSE '2077-01-01 23:00:00'
    END AS datumSORTIERUNG
    FROM ResourceFunctionAllocation
    JOIN ResourceAllocation On (ResourceFunctionAllocation.IdResourceFunctionAllocation = ResourceAllocation.IdResourceFunctionAllocation)

    )

    where 
    x.datumSORTIERUNG >= '2017-10-16T00:00:00.000' AND 
    x.datumSORTIERUNG <= '2017-10-16T23:59:59.999'
    ORDER BY x.datumSORTIERUNG

    HTH

    Grüße Alexander

    • Als Antwort vorgeschlagen Der Suchende Montag, 16. Oktober 2017 15:28
    • Als Antwort markiert jup0 Montag, 16. Oktober 2017 15:50
    Montag, 16. Oktober 2017 15:02

Alle Antworten

  • Hallo Jup0!

    Probier doch mal ein ORDER BY CASE...

    ORDER BY CASE WHEN UPPER(ResourceAllocation.Custom1) = 'IN' THEN ResourceFunctionAllocation.DateEnd
    WHEN UPPER(ResourceAllocation.Custom1) = 'OUT' THEN ResourceFunctionAllocation.DateStart
    ELSE '2077-01-01 23:00:00'
    END


    Einen schönen Tag noch, Christoph -- Data Platform MVP - http://www.insidesql.org/blogs/cmu

    Montag, 16. Oktober 2017 13:03
  • ich möchte jedoch in einer WHERE-Clausel den Zeitraum begrenzen, nicht nur sortieren...
    Montag, 16. Oktober 2017 13:08
  • Hallo ,
    versuche es einfach erneut zu schachteln:

    select x.*
    from
    (
    SELECT
    ResourceFunctionAllocation.DateStart AS datum,
    ResourceFunctionAllocation.DateEnd AS datumEND,
    CASE WHEN UPPER(ResourceAllocation.Custom1) = 'IN' THEN ResourceFunctionAllocation.DateEnd
    WHEN UPPER(ResourceAllocation.Custom1) = 'OUT' THEN ResourceFunctionAllocation.DateStart
    ELSE '2077-01-01 23:00:00'
    END AS datumSORTIERUNG
    FROM ResourceFunctionAllocation
    JOIN ResourceAllocation On (ResourceFunctionAllocation.IdResourceFunctionAllocation = ResourceAllocation.IdResourceFunctionAllocation)

    )

    where 
    x.datumSORTIERUNG >= '2017-10-16T00:00:00.000' AND 
    x.datumSORTIERUNG <= '2017-10-16T23:59:59.999'
    ORDER BY x.datumSORTIERUNG

    HTH

    Grüße Alexander

    • Als Antwort vorgeschlagen Der Suchende Montag, 16. Oktober 2017 15:28
    • Als Antwort markiert jup0 Montag, 16. Oktober 2017 15:50
    Montag, 16. Oktober 2017 15:02
  • Das Hauptproblem von Ausdrücken ist, dass man diese in Where-/GroupBy/On-Klauseln genau so wiederholen muss, da der Ergebnisname für den Select selber noch nicht zur Verfügung steht.

    Durch das bilden einer sog. "Derived Table => select x.* from (select ....) x" stehen die Ergebnisnamen dann zur Verfügung und können genau so wiederverwendet werden.

    Somit erleichtert Alexanders Beispiel die Schreibweisen, ändert aber nichts am Ergebnis.

    Montag, 16. Oktober 2017 15:28