简体   繁体   English

SQL对组中的某些元素进行计数,显示每个元素和总计数

[英]SQL Count certain elements in a group showing each element and total count

Imagine I have a table like this : 想象一下,我有一个这样的表:

+----+--------+--------+------+
| ID |  NAME  | DEPTNO | MGR  |
+----+--------+--------+------+
|  1 | AYEW   |     10 | 4    |
|  2 | JORDAN |     20 | 4    |
|  3 | JAMES  |     20 | 4    |
|  4 | MESSI  |     30 | NULL |
+----+--------+--------+------+

I need to display a result like this : 我需要显示这样的结果:

+----+---------+-------------+--------+-------+
| ID | MANAGER | SUBORDINATE | DEPTNO | COUNT |
+----+---------+-------------+--------+-------+
|  4 | MESSI   | AYEW        |     10 |     1 |
|  4 | MESSI   | JORDAN      |     20 |     2 |
|  4 | MESSI   | JAMES       |     20 |     2 |
+----+---------+-------------+--------+-------+

In other words, I have to count how many subordinates by department got each manager and also show the name and deptno of the subordinates. 换句话说,我必须计算每个部门有多少下属获得每个经理,还必须显示下属的姓名和部门名称。

I know how to easily associate the managers and subordinates with a JOIN but the problem is in the column of count. 我知道如何轻松地将管理人员和下属与JOIN相关联,但是问题出在计数栏中。 How can i count the total subordinates by department showing subordinates names at the same time ? 如何同时显示显示下属姓名的部门的下属总数? I assume I can't use GROUP BY to count the number of subordinates in each department so I have no idea how to do this. 我假设我无法使用GROUP BY来计算每个部门中下属的人数,所以我不知道该怎么做。

Figured out thanks to mathguy's answer it is also possible to do a second join like : 弄清楚了由于mathguy的答案,还可以像这样进行第二次联接:

join original_table c on (e.deptno=c.deptno and e.mgr=c.mgr)

so the count column will also show the intended result. 因此count列也将显示预期结果。

This is a join with analytic funtions: 这是带有分析功能的联接:

select m.id, m.name as manager, e.name as subordinate, e.deptno,
       count(*) over (partition by m.id, e.deptno) as cnt
from emps e join
     emps m
     on e.mgr = m.id;

Not sure how the analytic functions are implemented internally - it is possible Gordon's solution is EXACTLY the same as below. 不确定分析功能在内部如何实现-戈登的解决方案可能与下面的完全相同。 (Ordering of rows will probably be different - add an explicit ORDER BY if needed.) NOTE: I took your lead and named a column "count" - in general that is best avoided, as "count" is a reserved word (use "cnt" or something like it instead). (行的顺序可能会有所不同-如果需要,添加一个显式的ORDER BY。)注意:我率先将一列命名为“ count”-通常最好避免这种情况,因为“ count”是保留字(请使用“ cnt”或类似的名称)。

    select m.id, m.name, e.name, e.deptno, c.count 
    from emps e join emps m on e.mgr = m.id
    join (select mgr,deptno, count(*) count from emps where mgr is not null
                             group by mgr, deptno) c
    on e.mgr = c.mgr and e.deptno = c.deptno

    SQL> /
            ID NAME   NAME       DEPTNO      COUNT
    ---------- ------ ------ ---------- ----------
             4 MESSI  JORDAN         20          2
             4 MESSI  JAMES          20          2
             4 MESSI  AYEW           10          1
    3 rows selected.
    Elapsed: 00:00:00.01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM