获取正确数据的问题

发布于 2024-12-05 06:01:43 字数 2257 浏览 0 评论 0原文

我创建了一个如下所示的查询,但我还没有找到将部门分组在一起的方法。这就是我需要帮助的部分。我想在一个列中获取部门编号,员工是另一列,然后是另一列中的同事。任何帮助都会很棒。

这就是我想要的样子

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2024-12-12 06:01:43

听起来你想要类似的东西

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>>

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>>
请叫√我孤独 2024-12-12 06:01:43
 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
 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文