vendredi 1 février 2019

IF ELSE conditionals in SQL

I am trying to write a simple SQL query that selects either one field or another based on the value of a third field. Say I have 3 fields in table T_Ticket: Tier - a number between 1 and 4, Zone_T1 - a nvarchar(5) zone value of "A" to "F", and Zone_T234 - a nvarchar(5) of "G" to "M". I want to select Tier first and then, depending on the value of Tier (1 or 2, 3, 4) choose Zone_T1 if the value of Tier is 1, and Zone_T234 if its 2,3, or 4:

SELECT
   Tier,
   CASE
       WHEN Tier = 1
           THEN Zone_T1
       ELSE
           Zone_T234
FROM T_Ticket

What am I doing wrong here? I get a syntax error, but I am not sure whats wrong.

Aucun commentaire:

Enregistrer un commentaire