SQL - oracle - 如何从连接结果中选择特定部门

Posted

技术标签:

【中文标题】SQL - oracle - 如何从连接结果中选择特定部门【英文标题】:SQL - oracle - how to select specific department from join results 【发布时间】:2014-08-05 01:55:07 【问题描述】:

你们能帮我解决这个问题吗?我想对其进行修改,使其仅向我显示来自该查询的至少有 3 名员工的部门(占 78%),而不是来自(员工/部门的原始表)。每次我尝试“有 COUNT (department_ID) 或 @ WHERE 子句时都会给我一个错误。我需要进行第二次加入吗?谢谢

select *
from
(
 SELECT b.employee_id, b.employee_name,b.salary, a.department_id, 
 NVL(a.department_name, 'N/A') as dept_name,
 max(b.salary) over (partition by a.department_id) as max_sal
FROM        department a, employee b
WHERE       a.department_id(+) = b.department_id
) z
WHERE salary > (max_sal*.78 )

结果:

EMPLOYEE_ID EMPLOYEE_NAME SALARY DEPARTMENT_ID DEPT_NAME MAX_SAL


   7566 JONES                      3000            10 ACCOUNTING                 3000 
   7886 STEEL                      2500            10 ACCOUNTING                 3000 
   7944 LEE                        2400            20 RESEARCH                   3000 
   7999 WOLFE                      2500            20 RESEARCH                   3000 
   7610 WILSON                     3000            20 RESEARCH                   3000 
   7921 JACKSON                    2500            30 SALES                      3000 
   7900 FISHER                     3000            30 SALES                      3000 
   7788 SCOTT                      2500            40 IT                         2900 
   7910 SMITH                      2900            40 IT                         2900 
   7603 CLARK                      4000            50 EXECUTIVE                  5000 
   7596 JOST                       4500            50 EXECUTIVE                  5000 
   7839 KING                       5000            50 EXECUTIVE                  5000 
   8000 BREWSTER                   2500               N/A                        2500 

已选择 13 行

         This are RESULTS I need to get:

EMPLOYEE_ID EMPLOYEE_NAME SALARY DEPARTMENT_ID DEPT_NAME MAX_SAL


   7944 LEE                        2400            20 RESEARCH                   3000 
   7999 WOLFE                      2500            20 RESEARCH                   3000 
   7610 WILSON                     3000            20 RESEARCH                   3000 
   7603 CLARK                      4000            50 EXECUTIVE                  5000 
   7596 JOST                       4500            50 EXECUTIVE                  5000 
   7839 KING                       5000            50 EXECUTIVE                  5000 

已选择 6 行

【问题讨论】:

【参考方案1】:

您可以在另一个子查询中使用解析函数进行计算:

select de.*
from (select de.*, count(*) over (partition by department_id) as cnt
      from (SELECT e.employee_id, e.employee_name, d.salary, d.department_id, 
                   NVL(d.department_name, 'N/A') as dept_name,
                   max(e.salary) over (partition by d.department_id) as max_sal
            FROM department d JOIN
                 employee e
                 ON d.department_id = e.department_id
           ) de
      where salary > max_sal*.78
     ) de
where cnt >= 3;

外连接似乎没有必要,所以我用内连接和现代join 语法替换了它。我还将表别名更改为表的缩写。它使代码更易于阅读。

【讨论】:

非常感谢。我看到使用过度分区两次可以让您保持计数,这使我无法得到答案再次感谢您...祝您晚安!

以上是关于SQL - oracle - 如何从连接结果中选择特定部门的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Java 中的 Oracle SQL 选择中获取原始脚本输出而不是查询结果

如何连接具有选择性重复记录的表? (oracle10g)

如何从 oracle sql 导出结果到 excel? [复制]

从结果表中多选到oracle中的单行

java连接Oracle数据库,从ResultSet中提取数据出现java.sql.sqlException结果集已耗尽

在 Oracle 中连接 SQL 查询的结果