CNB CNB - 6 months ago 19
MySQL Question

Self-refering table. MySQL

I have another question. I don't seem to fully grasp the concept.

I have a table:

EMPLOYEES

| EMP_NO | APELLIDO | OFFICE | DIRECTOR | START_DATE | SALARY | COMMISSION | DEP_NO |


I have to obtain data of employees who's OFFICE is 'DIRECTOR' (easy, but... ) and I also have to include a column that would return full salary (salary+commission) of all the employees who are theirs subordinates.

Now I know that I need to 'copy' the table so I am able to make it to refer to itself (EMPLOYEES e1, EMPLOYEES e2 etc.). But I am getting really worked up in that obtaining total of salary of those subordinates.

Any thoughts?
(pls help)

EDIT

Sorry guys, true. My fault. OK, so here are some sample data:

+--------+----------+------------+----------+------------+---------+----------+--------+
| EMP_NO | SURNAME | OFFICE | DIRECTOR | START_DATE | SALARY |COMMISSION| DEP_NO |
+--------+----------+------------+----------+------------+---------+----------+--------+
| 7499 | ALONSO |SALESPERSON | 7698 | 1981-02-23 | 1400.00 | 400.00 | 30 |
| 7521 | LOPEZ | EMPLOYEE | 7782 | 1981-05-08 | 1350.50 | NULL | 10 |
| 7654 | MARTIN |SALESPERSON | 7698 | 1981-09-28 | 1500.00 | 1600.00 | 30 |
| 7698 | GARRIDO | DIRECTOR | 7839 | 1981-05-01 | 3850.12 | NULL | 30 |
| 7782 | MARTINEZ | DIRECTOR | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7839 | REY | CEO | NULL | 1981-11-17 | 6000.00 | NULL | 10 |
| 7844 | CALVO |SALESPERSON | 7698 | 1981-09-08 | 1800.00 | 0.00 | 30 |
| 7876 | GIL | ANALIST | 7782 | 1982-05-06 | 3350.00 | NULL | 20 |
| 7900 | JIMENEZ | EMPLOYEE | 7782 | 1983-03-24 | 1400.00 | NULL | 20 |
+--------+----------+------------+----------+------------+---------+----------+--------+


What I need to achieve now is to return table with details of employees GARRIDO and MARTINEZ (EMP_NO 7698 and 7782) along with additional column that would contain total salary of all theirs direct subordinates. Something like that:

+--------+----------+------------+----------+------------+---------+----------+--------+-----------+
| EMP_NO | SURNAME | OFICIO | DIRECTOR | FECHA_ALTA | SALARIO | COMISION | DEP_NO | TOTAL_EMP |
+--------+----------+------------+----------+------------+---------+----------+--------+-----------+
| 7698 | GARRIDO | DIRECTOR | 7839 | 1981-05-01 | 3850.12 | NULL | 30 | 6700 |
| 7782 | MARTINEZ | DIRECTOR | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | 1350.50 |
+--------+----------+------------+----------+------------+---------+----------+--------+-----------+

Answer

I believe this should do it. You just need to JOIN to the other rows that you need, then it's a simple matter of grouping to get the aggregate amount that you want.

SELECT
    D.emp_no,
    D.apellido,    -- Why is there one column named in Spanish and the rest in English?
    D.office,
    D.director,
    D.start_date,
    D.salary,
    D.commission,
    D.dep_no,
    SUM(COALESCE(S.salary, 0) + COALESCE(S.commission, 0)) AS subordinates_compensation
FROM
    Employees D
LEFT OUTER JOIN Employees S ON S.director = D.emp_no
WHERE
    D.office = 'Director'
GROUP BY
    D.emp_no,
    D.apellido,
    D.office,
    D.director,
    D.start_date,
    D.salary,
    D.commission,
    D.dep_no
Comments