列出每个部门的员工

时间:2018-03-25 10:38:34

标签: sql oracle11g oracle10g

我正在使用两个表,dept(部门)和emp(员工)。

这是部门的描述。

Name                                      Null?    Type
DEPTNO                                    NOT NULL NUMBER(2)
DNAME                                              VARCHAR2(14)
LOC                                                VARCHAR2(13)

这是对emp的描述。

 Name                                      Null?    Type
 EMPNO                                     NOT NULL NUMBER(4)
 ENAME                                              VARCHAR2(10)
 JOB                                                VARCHAR2(9)
 MGR                                                NUMBER(4)
 HIREDATE                                           DATE
 SAL                                                NUMBER(7,2)
 COMM                                               NUMBER(7,2)
 DEPTNO                                             NUMBER(2)

现在,我想列出每个部门的所有员工并生成如下输出:

  DEPTNO DNAME              LOC           
---------- -------------- ------------- 
    10 ACCOUNTING     NEW YORK      
 EMPNO ENAME
---------- ----------
  7782 CLARK
  7839 KING
  7934 MILLER


    20 RESEARCH       DALLAS        
 EMPNO ENAME
---------- ----------
  7369 SMITH
  7566 JONES
  7788 SCOTT
  7876 ADAMS
  7902 FORD


    30 SALES          CHICAGO       
 EMPNO ENAME
---------- ----------
  7499 ALLEN
  7521 WARD
  7654 MARTIN
  7698 BLAKE
  7844 TURNER
  7900 JAMES

    40 OPERATIONS     BOSTON 
    50 MAINTENANCE    DALLAS

最后两个部门没有员工。到目前为止,我已经提出了这个问题:

 select d.deptno, d.dname, d.loc, e.empno, e.ename
 from dept d left join emp e on (d.deptno = e.deptno)
 order by d.deptno;

这给了我以下结果:

DEPTNO DNAME          LOC                EMPNO ENAME
---------- -------------- ------------- ---------- ----------
    10 ACCOUNTING     NEW YORK            7782 CLARK
    10 ACCOUNTING     NEW YORK            7839 KING
    10 ACCOUNTING     NEW YORK            7934 MILLER
    20 RESEARCH       DALLAS              7566 JONES
    20 RESEARCH       DALLAS              7788 SCOTT
    20 RESEARCH       DALLAS              7369 SMITH
    20 RESEARCH       DALLAS              7876 ADAMS
    30 SALES          CHICAGO             7499 ALLEN
    30 SALES          CHICAGO             7900 JAMES
    30 SALES          CHICAGO             7844 TURNER
    30 SALES          CHICAGO             7521 WARD
    30 SALES          CHICAGO             7698 BLAKE
    30 SALES          CHICAGO             7654 MARTIN
    40 OPERATIONS     BOSTON
    50 MAINTENANCE    DALLAS

如您所见,它列出了每个记录的部门编号和名称,而我希望它只显示一次。我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

这是我可以解决的最接近的结果。

select case when rn = 1 then deptno else null end deptno,
       case when rn = 1 then dname else null end dname,
       case when rn = 1 then loc else null end loc,
       empno, 
       ename 
  from (select d.deptno, 
               d.dname, 
               d.loc, 
               e.empno, 
               e.ename,
               row_number() over(partition by d.deptno order by e.empno) rn,
               row_number() over(order by d.deptno, e.empno) fullrn
          from dept d 
               left join 
               emp e 
            on (d.deptno = e.deptno))
order by fullrn;

输出:

DEPTNO  DNAME       LOC        EMPNO    ENAME
10      ACCOUNTING  NEW YORK   7782     CLARK
 -       -           -         7839     KING
 -       -           -         7934     MILLER
20      RESEARCH    DALLAS     7369     SMITH
 -       -           -         7566     JONES
 -       -           -         7788     SCOTT
 -       -           -         7876     ADAMS
 -       -           -         7902     FORD
30      SALES       CHICAGO    7499     ALLEN
 -       -           -         7521     WARD
 -       -           -         7654     MARTIN
 -       -           -         7698     BLAKE
 -       -           -         7844     TURNER
 -       -           -         7900     JAMES
40      OPERATIONS  BOSTON      -        - 
50      MAINTENANCE DALLAS      -        -