MySQL aggregates different groups/partitions

Note: The test database version is MySQL 8.0

If you need scott users to create tables and enter data statements, please refer to:
scott create tables and enter data sql scripts

1. Demand

At the same time, gather by different dimensions.

For example, to return such a result set: list the name of each employee, his department, the number of employees in the department (including himself), the number of employees with the same position as him (including himself), and the emp table The total number of employees in.

The result set should be as follows:
±-------±-------±-----------±----------±-- ------±------+
| ename | deptno | deptno_cnt | job | job_cnt | total |
±-------±-------±------ -----±----------±--------±------+
| SCOTT | 20 | 5 | ANALYST | 2 | 14 |
| FORD | 20 | 5 | ANALYST | 2 | 14 |
| MILLER | 10 | 3 | CLERK | 4 | 14 |
| SMITH | 20 | 5 | CLERK | 4 | 14 |
| ADAMS | 20 | 5 | CLERK | 4 | 14 |
| JAMES | 30 | 6 | CLERK | 4 | 14 |
| CLARK | 10 | 3 | MANAGER | 3 | 14 |
| JONES | 20 | 5 | MANAGER | 3 | 14 |
| BLAKE | 30 | 6 | MANAGER | 3 | 14 |
| KING | 10 | 3 | PRESIDENT | 1 | 14 |
| ALLEN | 30 | 6 | SALESMAN | 4 | 14 |
| WARD | 30 | 6 | SALESMAN | 4 | 14 |
| MARTIN | 30 | 6 | SALESMAN | 4 | 14 |
| TURNER | 30 | 6 | SALESMAN | 4 | 14 |
±-------±-------±-----------±----------±--------±------+

2. Solution

Window functions make this problem fairly easy to solve. If you cannot use window functions, you can also use scalar subqueries.

select  ename,
        deptno,
        count(*) over w1 as 'deptno_cnt',
        job,
        count(*) over w2 as 'job_cnt',
        count(*) over w3 as 'total'
  from  emp
window w1 as (partition by deptno),
        w2 as (partition by job),
        w3 as ()

Test Record:

mysql> select  ename,
    ->         deptno,
    ->         count(*) over w1 as 'deptno_cnt',
    ->         job,
    ->         count(*) over w2 as 'job_cnt',
    ->         count(*) over w3 as 'total'
    ->   from  emp
    -> window w1 as (partition by deptno),
    ->         w2 as (partition by job),
    ->         w3 as ();
+--------+--------+------------+-----------+---------+-------+
| ename  | deptno | deptno_cnt | job       | job_cnt | total |
+--------+--------+------------+-----------+---------+-------+
| SCOTT  |     20 |          5 | ANALYST   |       2 |    14 |
| FORD   |     20 |          5 | ANALYST   |       2 |    14 |
| MILLER |     10 |          3 | CLERK     |       4 |    14 |
| SMITH  |     20 |          5 | CLERK     |       4 |    14 |
| ADAMS  |     20 |          5 | CLERK     |       4 |    14 |
| JAMES  |     30 |          6 | CLERK     |       4 |    14 |
| CLARK  |     10 |          3 | MANAGER   |       3 |    14 |
| JONES  |     20 |          5 | MANAGER   |       3 |    14 |
| BLAKE  |     30 |          6 | MANAGER   |       3 |    14 |
| KING   |     10 |          3 | PRESIDENT |       1 |    14 |
| ALLEN  |     30 |          6 | SALESMAN  |       4 |    14 |
| WARD   |     30 |          6 | SALESMAN  |       4 |    14 |
| MARTIN |     30 |          6 | SALESMAN  |       4 |    14 |
| TURNER |     30 |          6 | SALESMAN  |       4 |    14 |
+--------+--------+------------+-----------+---------+-------+
14 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/u010520724/article/details/113985289