1

我必须按照以下规则对 EMPLOYEE 表中的员工进行排序:当他们的部门是 SALES 时,按经验排序,否则按 SALARY 排序

在此处输入图像描述

这是我尝试过的:

select FNAME||' '||LNAME as emp_name,salary,HIRE_DATE from employee 
where dept_id=(select dept_id from department where name='SALES') order by HIRE_DATE asc;
UNION
select FNAME||' '||LNAME as emp_name,salary,HIRE_DATE from employee
where dept_id NOT IN (select dept_id from department where name='SALES') order by salary desc;

由于我认为有两个订单,这会引发错误。有什么解决办法吗?

4

4 回答 4

3

试试这个:

select FNAME||' '||LNAME as emp_name,salary,HIRE_DATE from employee  E
join department D
on E.dept_id=D.dept_id
 order by (case when D.name='SALES' then HIRE_DATE end),
           (case when D.name<>'SALES' then salary end) desc
于 2012-07-18T10:24:27.873 回答
2

首先显示销售部门,按hire_date 排序。其他部门按工资排序显示。

select FNAME||' '||LNAME as emp_name,salary,HIRE_DATE 
  from employee
 inner join department
    on employee.dept_id = department.dept_id
 order by
 -- Sales department is on top. Swap 0 and 1 if you want sales on bottom
       case when department.name = 'SALES'
            then 0
            else 1
       end,
 -- And employees are sorted by hire_date
       case when department.name = 'SALES'
            then employee.HIRE_DATE
            else null
       end,
 -- Employees from other departments are sorted by salary
       employee.Salary desc
于 2012-07-18T10:26:29.253 回答
1

使用解码:

SELECT   fname || ' ' || lname AS emp_name
        ,salary
        ,hire_date
    FROM employee e
        ,department d
   WHERE e.dept_id = d.dept_id
ORDER BY decode(d.name,'SALES',experience,salary)
;

另见http://sqlfiddle.com/#!4/afa6f/1

于 2012-07-18T10:23:24.437 回答
0
SELECT e.fname||' '||e.lname as emp_name, e.salary, e.hire_date
FROM employee e, department d
WHERE e.dept_id = d.dept_id 
ORDER BY
CASE 
WHEN d.name ='SALES' then
hire_date ASC
ELSE
salary DESC
END 

好的。抱歉,试试这个 - 可能已经回答过了。

于 2012-07-18T10:29:22.840 回答