显示公司员工分布时出错

时间:2018-04-16 22:38:31

标签: sql-server

我有两张桌子:[BRANCH_T]和[EMPLOYEE_T]。

BRANCH_T包含branch_id和manager_emp_id(外键)。

在EMPLOYEE_T中包含emp_id,emp_firstname,emp_lastname,emp_position,emp_gender和branch_id(外键)。

我想展示branch_id,每个分支的经理名称,主管总人数,男性员工总数和女性员工总数。

以下是我的代码:

SELECT b.bch_id AS 'BRANCH NUMBER',
(CASE WHEN emp_position = 'M' 
THEN emp_fname + ' ' + emp_lname ELSE '-' END) AS 'MANAGER NAME',
SUM (CASE WHEN e.emp_position = 'S' THEN 1 ELSE 0 END) AS 'TOTAL NUMBER OF SUPERVISOR',
SUM (CASE WHEN e.emp_gender = 'M' THEN 1 ELSE 0 END) AS 'TOTAL MALE EMPPLOYEES',
SUM (CASE WHEN e.emp_gender = 'F' THEN 1 ELSE 0 END) AS 'TOTAL FEMALE EMPLOYEES'
FROM EMPLOYEE_T e, BRANCH_T b
WHERE b.bch_id = e.bch_id
GROUP BY b.bch_id, e.emp_fname, e.emp_lname, e.emp_gender, e.emp_position

这是生成的结果。

+---------------+--------------+-----------------------------+----------------------+------------------------+
| branch number | manager name | total number of supervisors | total male employees | total female employees |
+---------------+--------------+-----------------------------+----------------------+------------------------+
| B001          | EMILIA MOANA |                           0 |                    0 |                      1 |
| B001          | -            |                           1 |                    1 |                      0 |
| B001          | -            |                           0 |                    0 |                      1 |
| B002          | MARCUS HOW   |                           1 |                    0 |                      1 |
+---------------+--------------+-----------------------------+----------------------+------------------------+

我尝试了另一个代码,但是它出错并且无法执行。

SELECT  b.bch_id AS 'BRANCH NUMBER',
b.mng_emp_id AS 'MANAGER ID',
(SELECT e.emp_fname + ' ' + e.emp_lname
FROM EMPLOYEE_T e, BRANCH_T b
WHERE   b.mng_emp_id = e.emp_id and
b.bch_id = e.bch_id
group by e.emp_fname, e.emp_lname, e.bch_id) AS 'MANAGER NAME',
SUM (CASE WHEN e.emp_position = 'S' THEN 1 ELSE 0 END) AS 'NUMBER OF SUPERVISOR',
SUM (CASE WHEN e.emp_gender = 'M' THEN 1 ELSE 0 END) AS 'NUMBER OF MALE EMPLOYEES',
SUM (CASE WHEN e.emp_gender = 'F' THEN 1 ELSE 0 END) AS 'NUMBER OF FEMALE EMPLOYEES'
FROM EMPLOYEE_T e, BRANCH_T b
WHERE   e.bch_id = b.bch_id
GROUP BY b.bch_id, b.mng_emp_id, e.emp_position, e.emp_gender

错误代码:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我想要展示的是这样的:

+---------------+--------------+-----------------------------+----------------------+------------------------+
| branch number | manager name | total number of supervisors | total male employees | total female employees |
+---------------+--------------+-----------------------------+----------------------+------------------------+
| B001          | EMILIA MOANA |                           1 |                    1 |                      2 |
| B002          | MARCUS HOW   |                           1 |                    2 |                      1 |
| B003          | ELINA YEAN   |                           1 |                    1 |                      1 |
+---------------+--------------+-----------------------------+----------------------+------------------------+

请帮忙。谢谢。

---------------- UPDATE ----------------

我可以成功检索每个分支机构的主管,男性和女性员工的数量,但我只能从BRANCH_T获取mng_emp_id。

SELECT  b.bch_id AS 'BRANCH NUMBER',
b.mng_emp_id AS 'MANAGER ID',
SUM (CASE WHEN e.emp_position = 'S' THEN 1 ELSE 0 END) AS 'NUMBER OF SUPERVISOR',
SUM (CASE WHEN e.emp_gender = 'M' THEN 1 ELSE 0 END) AS 'NUMBER OF MALE EMPLOYEES',
SUM (CASE WHEN e.emp_gender = 'F' THEN 1 ELSE 0 END) AS 'NUMBER OF FEMALE EMPLOYEES'
FROM EMPLOYEE_T e, BRANCH_T b
WHERE   e.bch_id = b.bch_id
GROUP BY b.bch_id, b.mng_emp_id

mng_emp_id是emp_id的一部分。

BRANCH_T:

+--------+------------+
| BCH_ID | MNG_EMP_ID |
+--------+------------+
| B001   | EM001      |
| B002   | EM002      |
| B003   | EM003      |
+--------+------------+

EMPLOYEE_T:

+--------+-----------+-----------+--------+
| EMP_ID | EMP_FNAME | EMP_LNAME | BCH_ID |
+--------+-----------+-----------+--------+
| EM001  | A         | Z         | B001   |
| EM002  | B         | Y         | B002   |
| EM003  | C         | X         | B003   |
| ES001  | D         | W         | B001   |
| ET001  | E         | V         | B002   |
+--------+-----------+-----------+--------+

0 个答案:

没有答案