获取正确数据的问题
我创建了一个如下所示的查询,但我还没有找到将部门分组在一起的方法。这就是我需要帮助的部分。我想在一个列中获取部门编号,员工是另一列,然后是另一列中的同事。任何帮助都会很棒。
这就是我想要的样子
DEPARTMENT EMPLOYEE COLLEAGUE
10 CLARK KING
10 CLARK MILLER
10 KING CLARK
10 KING MILLER
10 MILLER CLARK
10 MILLER KING
20 ADAMS FORD
20 ADAMS JONES
20 ADAMS SCOTT
20 ADAMS SMITH
这是我到目前为止所得到的,但不正确。国王的同事不是国王,而是克拉克和米勒。克拉克的同事不是克拉克而是国王和米勒。
SQL> select ename AS Employee, deptno AS Department, ename AS Colleague from emp order by deptno asc;
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
KING 10 KING
CLARK 10 CLARK
MILLER 10 MILLER
ADAMS 20 ADAMS
SCOTT 20 SCOTT
SMITH 20 SMITH
FORD 20 FORD
JONES 20 JONES
WARD 30 WARD
JAMES 30 JAMES
ALLEN 30 ALLEN
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
MARTIN 30 MARTIN
BLAKE 30 BLAKE
TURNER 30 TURNER
14 rows selected.
这是 dept 表
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
这是 emp 表
SQL> select ename, deptno, job from emp;
ENAME DEPTNO JOB
---------- ---------- ---------
KING 10 PRESIDENT
BLAKE 30 MANAGER
CLARK 10 MANAGER
JONES 20 MANAGER
MARTIN 30 SALESMAN
ALLEN 30 SALESMAN
TURNER 30 SALESMAN
JAMES 30 CLERK
WARD 30 SALESMAN
FORD 20 ANALYST
SMITH 20 CLERK
ENAME DEPTNO JOB
---------- ---------- ---------
SCOTT 20 ANALYST
ADAMS 20 CLERK
MILLER 10 CLERK
14 rows selected.
I have created a query that looks like this but I havent found a way to group the departments together. Thats the part I need help with. I want to get Department number in one colum, Employee is another and then the person that would be their colleague in another. Any help would be great.
This is what I want it to look like
DEPARTMENT EMPLOYEE COLLEAGUE
10 CLARK KING
10 CLARK MILLER
10 KING CLARK
10 KING MILLER
10 MILLER CLARK
10 MILLER KING
20 ADAMS FORD
20 ADAMS JONES
20 ADAMS SCOTT
20 ADAMS SMITH
And here is what I got so far but is not correct.Kings colleague is not King its Clark and Miller. Clarks colleague is not Clark its king and Miller.
SQL> select ename AS Employee, deptno AS Department, ename AS Colleague from emp order by deptno asc;
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
KING 10 KING
CLARK 10 CLARK
MILLER 10 MILLER
ADAMS 20 ADAMS
SCOTT 20 SCOTT
SMITH 20 SMITH
FORD 20 FORD
JONES 20 JONES
WARD 30 WARD
JAMES 30 JAMES
ALLEN 30 ALLEN
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
MARTIN 30 MARTIN
BLAKE 30 BLAKE
TURNER 30 TURNER
14 rows selected.
Heres the dept table
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Heres the emp table
SQL> select ename, deptno, job from emp;
ENAME DEPTNO JOB
---------- ---------- ---------
KING 10 PRESIDENT
BLAKE 30 MANAGER
CLARK 10 MANAGER
JONES 20 MANAGER
MARTIN 30 SALESMAN
ALLEN 30 SALESMAN
TURNER 30 SALESMAN
JAMES 30 CLERK
WARD 30 SALESMAN
FORD 20 ANALYST
SMITH 20 CLERK
ENAME DEPTNO JOB
---------- ---------- ---------
SCOTT 20 ANALYST
ADAMS 20 CLERK
MILLER 10 CLERK
14 rows selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你想要类似的东西
It sounds like you want something like