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 开发者_如何学C 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.
It sounds like you want something like
SQL> ed
Wrote file afiedt.buf
1 select deptno department,
2 e1.ename employee,
3 e2.ename colleague
4 from emp e1 join emp e2 using (deptno)
5 where e1.empno != e2.empno
6* order by deptno
SQL> /
DEPARTMENT EMPLOYEE COLLEAGUE
---------- ---------- ----------
10 MILLER CLARK
10 KING CLARK
10 MILLER KING
10 CLARK MILLER
10 KING MILLER
10 CLARK KING
<<additional results removed>>
Select distinct d.dname, t1.employee, t2.employee college
From emp t1
inner join dept d on d.deptno = t1.deptno
Inner join emp t2 on t2.deptno = t1.deptno and t1.employee <> t2.employee
精彩评论