简体   繁体   English

SQL-Oracle:解决基本问题的难度,第3部分

[英]SQL-Oracle: Difficulties in resolving basic problems, part3

So here it is...Produce a list of jobs for departments 10, 50, 20, in that order. 所以这里是...按顺序生成部门10、50、20的工作清单。 Display the job ID and department ID by using the set operators. 通过使用设置的运算符显示作业ID和部门ID。 I have done this so far but i can't do the ordering properly(10,50,20), any ideas?...thanks 到目前为止,我已经完成了此操作,但是我无法正确进行订购(10、50、20),有什么想法吗?...谢谢

SELECT job_id, department_id
FROM (SELECT job_id, department_id
      FROM   employees
      WHERE  department_id IN ('10','20','50')
      INTERSECT
      SELECT job_id, department_id
      FROM   employees
      WHERE  department_id IN ('10','20','50')
      )    
GROUP BY department_id, job_id
ORDER BY COUNT(job_id);

the output is this: 输出是这样的:

job_ID      department_id

ST_CLERK     50
ST_MAN       50
MK_REP       20
SH_CLERK     50
AD_ASST      10
MK_MAN       20

You can use a CASE statement to implement arbitrary ordering 您可以使用CASE语句来实现任意排序

ORDER BY (CASE department_id
               WHEN 10 THEN 1
               WHEN 50 THEN 2
               WHEN 20 THEN 3
               ELSE 4
           END) ASC

As a note, you could also do instr() : 注意,您也可以执行instr()

order by instr(',10,50,20,', ',' || department_id || ',')

The extra commas are to ensure that there are no problems with similar substrings ("110" and "10"). 多余的逗号是为了确保类似的子字符串(“ 110”和“ 10”)没有问题。 That is not a problem for these particular values, so you could do: 对于这些特定值,这不是问题,因此您可以执行以下操作:

order by instr('10,50,20', department_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM