SQL修改返回的选择值没有union

时间:2017-10-20 14:46:23

标签: sql select oracle-sqldeveloper

我正在尝试编写一个SQL语句,该语句按员工编号,名称,job_id和修改后的薪水返回员工列表,具体取决于他们的job_id。如果员工是副总裁,则SELECT语句应返回增加%20的薪水,但如果他们是MAN,则应返回增加%30的薪水。没有工会可以返回两个不同的修改工资吗?这是我到目前为止所拥有的。

SELECT 'Employee ', EMPLOYEE_ID, 
'named ', FIRST_NAME || ' ' || LAST_NAME AS "Full Name", 
'with Job ID of ', JOB_ID,
'will have a new salary of ', SALARY
FROM EMPLOYEES
WHERE ROUND(SALARY*1.2,2) IN (SELECT ROUND(SALARY*1.2,2)
FROM EMPLOYEES
WHERE JOB_ID LIKE '%VP')
OR ROUND(SALARY*1.3,2) IN (SELECT ROUND(SALARY*1.3,2)
FROM EMPLOYEES
WHERE JOB_ID LIKE '%MAN');

2 个答案:

答案 0 :(得分:1)

它更简单:

SELECT 
  'Employee ',
  employee_id, 
  'named ', 
  first_name || ' ' || last_name AS "Full Name", 
  'with Job ID of ',
  job_id,
  'will have a new salary of ', 
  CASE 
    WHEN job_id LIKE '%VP' THEN salary * 1.2
    WHEN job_id LIKE '%MAN' THEN salary * 1.3
    ELSE salary
  END AS modified_salary
FROM employees;

答案 1 :(得分:1)

您可以使用case表达式:

SELECT 'Employee ', EMPLOYEE_ID, 
       'named ', FIRST_NAME || ' ' || LAST_NAME AS "Full Name", 
       'with Job ID of ', JOB_ID,
       'will have a new salary of ',
       salary * CASE WHEN job_id LIKE '%VP' THEN 1.2
                     WHEN job_id LIKE '%MAN' THEN 1.3
                     ELSE 1
                END
FROM   EMPLOYEES