Home > Software engineering >  MySQL- How to use alias with CASE statement
MySQL- How to use alias with CASE statement

Time:06-28

I want to display First Name, Salary, Tenure and Bonus based on tenure but I am getting this error:

"TENURE": invalid identifier

This is my query:

select 
first_name as "First Name", Salary, 
round((sysdate - to_date(hire_date))/365.25,2) as Tenure,
Salary*(
CASE
when Tenure between 0 and 10 then 0.05
when Tenure between 11 and 15 then 0.1
when Tenure between 16 and 20 then 0.15
when Tenure > 20 then 0.2 
END) as Bonus
from employees;

CodePudding user response:

Try this.

SELECT AA.first_name as "First Name", AA.Salary,
AA.Salary*(
CASE
     when AA.Tenure between 0 and 10 then 0.05
     when AA.Tenure between 11 and 15 then 0.1
     when AA.Tenure between 16 and 20 then 0.15
     when AA.Tenure > 20 then 0.2 
END) as Bonus
FROM (select 
first_name, Salary, 
round((sysdate - to_date(hire_date))/365.25,2) as Tenure
from employees)) AA;
  • Related