lundi 1 juin 2015

Calculated column in SQL with IF-ELIF-ELSE condition

Dear Stackoverflow users,

I need help on how to calculate this calculated column in SQL-code. I have seen earlier in the forum how you calculate based on adding columns together, or just giving a standard value. But in my case, I would like to add a column that could show different pre-set values based on the values of another column. It is just like a normal IF-ELIF-ELSE-statement.

In my case, I have a database, a table, that logs every change one of the users do in the system. Whenever a change is made, the users employee-number is logged to the table and not the employee's name. Since we spend a lot of time going back to these logs, we spend a lot of time looking up which employer-number belong to who. I would like this to be done directly in the query. I have a list of all the employee-numbers and their corresponding names.

My basic SQL-skill only tell me how to import table/columns and create one, but with the IF-statement I am completely lost, below I have performed some pseudo-code so you might understand what I am looking for. A.Emp_Num is the column with the employee numbers and Emp_Name a created column with 'null' as content.

SELECT A.EMP_NUM, A.COLUMN1, A.COLUMN2, 'Null' AS EMP_NAME

FROM LOGGINGTABLE A

IF A.EMP_NUM=='1234' THEN EMP_NAME='MONICA'

ELIF A.EMP_NUM=='5678' THEN EMP_NAME='JOE'

Thanks for all help!

Carl

Aucun commentaire:

Enregistrer un commentaire