mysql指望来自另一个表的多个列

时间:2018-05-03 07:26:49

标签: mysql

我想从另一个表中获取两列的数量。例如,源数据如下所示,名为employee_master

emp_code          dept_id          sub_dept_id
1                   10                  22
2                   11                  20
3                   10                  22
4                   10                  22
5                   11                  20
6                   10                  21 

另一个名为punching_data_table的源表,如下所示,与employee_master连接。

emp_code          present
1                   10:01 AM
2                   10:02 AM
3                   10:02 AM
4                   10:03 AM
5                   10:11 AM
6                   10:09 AM      //biometric punching time shown

结果假设如下所示。

dept_id     dept_count     sub_dept_id      sub_dept_count
10          4              22               3
10          4              21               1
11          2              20               2

尝试使用以下代码,但即使由employee_master.dept_id,employee_master.sub_dept_id应用了组,它也不会单独计算两列。请注意,两个表之间只有一列是emp_code。

SELECT p.emp_code
     , e.emp_code
     , COUNT(e.dept_id) dept_id_count
     , COUNT(e.sub_dept_id) sub_dept_id_count
  FROM punching_data_table p
  LEFT 
  JOIN employee_master e
    ON e.emp_code = p.emp_code
 GROUP 
    BY e.dept_id
     , e.sub_dept_id

对此问题的任何帮助都将给予高度赞赏。

4 个答案:

答案 0 :(得分:0)

尝试此查询

SELECT emp_code, MAX(dept_id_count) as dept_id_count,  MAX(sub_dept_id_count) as sub_dept_id_count
FROM
(SELECT p.emp_code  , COUNT(e.dept_id) dept_id_count, 0 as sub_dept_id_count
FROM punching_data_table p
LEFT JOIN employee_master e ON e.emp_code = p.emp_code
GROUP BY e.dept_id
UNION
SELECT p.emp_code, 0 as dept_id_count, COUNT(e.sub_dept_id) sub_dept_id_count
FROM punching_data_table p
LEFT JOIN employee_master e ON e.emp_code = p.emp_code
GROUP BY e.sub_dept_id) as subQuery
GROUP BY emp_code

答案 1 :(得分:0)

此查询(SQLFiddle)将为您提供您指定的输出:

SELECT em1.dept_id, em2.dept_count, em1.sub_dept_id, COUNT(em1.sub_dept_id) AS sub_dept_count
FROM employee_master em1
JOIN (SELECT dept_id, COUNT(dept_id) as dept_count 
      FROM employee_master e
      JOIN punching_data_table p
      ON e.emp_code = p.emp_code
      GROUP BY dept_id) em2
ON em1.dept_id = em2.dept_id
WHERE EXISTS (SELECT * FROM punching_data_table pdt WHERE pdt.emp_code = em1.emp_code)
GROUP BY dept_id, sub_dept_id

输出:

dept_id     dept_count  sub_dept_id     sub_dept_count
10          4           21              1
10          4           22              3
11          2           20              2

如果您有部门名称表,例如

CREATE TABLE dept_master (dept_id INT, dept_name VARCHAR(20));

INSERT INTO dept_master VALUES
(10, 'Department 10'),
(11, 'Department 11');

您也可以使用此查询(SQLFiddle)加入:

SELECT d.dept_name, em2.dept_count, em1.sub_dept_id, COUNT(em1.sub_dept_id) AS sub_dept_count
FROM employee_master em1
JOIN (SELECT dept_id, COUNT(dept_id) as dept_count 
      FROM employee_master e
      JOIN punching_data_table p
      ON e.emp_code = p.emp_code
      GROUP BY dept_id) em2
ON em1.dept_id = em2.dept_id
JOIN dept_master d on d.dept_id = em1.dept_id
WHERE EXISTS (SELECT * FROM punching_data_table pdt WHERE pdt.emp_code = em1.emp_code)
GROUP BY d.dept_id, sub_dept_id

然后在输出中获取部门名称而不是ID:

dept_name       dept_count  sub_dept_id     sub_dept_count
Department 10   4           21              1
Department 10   4           22              3
Department 11   2           20              2

答案 2 :(得分:0)

在您的问题中有一些不明确的事情,例如,您是否需要不同的员工或只是员工的出现,如果员工在punching_data中不存在会发生什么。我在这个答案中做了一些假设,但其关键在于你创建了2个子查询并将它们加入dept id,这将强制进行1对多的连接

所以给出

drop table if exists employee_master,punching_data_table;

create table employee_master
(emp_code int,         dept_id  int,        sub_dept_id int);
insert into employee_master values
(1       ,            10   ,               22),
(2       ,            11   ,               20),
(3       ,            10   ,               22),
(4       ,            10   ,               22),
(5       ,            11   ,               20),
(6       ,            10   ,               21 ),
(7       ,            10   ,               21);

create table punching_data_table(emp_code int);
insert into punching_data_table values          
(1),                   
(2),                 
(3),                   
(4) ,                  
(5),
(6);

select a.dept_id,a.empcodes,s.sub_dept_id,s.empcodes from
(
select  dept_id ,count(distinct emp_code) empcodes
from employee_master
where emp_code in (select emp_code from punching_data_table)
group by dept_id
) a

join

(select dept_id , sub_dept_id, count(distinct emp_code)  empcodes
from employee_master
where emp_code in (select emp_code from punching_data_table)
group by dept_id,sub_dept_id) s
on s.dept_id = a.dept_id
order by a.dept_id,s.sub_dept_id

结果

+---------+----------+-------------+----------+
| dept_id | empcodes | sub_dept_id | empcodes |
+---------+----------+-------------+----------+
|      10 |        4 |          21 |        1 |
|      10 |        4 |          22 |        3 |
|      11 |        2 |          20 |        2 |
+---------+----------+-------------+----------+
3 rows in set (0.00 sec)

答案 3 :(得分:0)

无需punching_data_table

即可实现此结果
SELECT t1.dept_id, 
       t2.sub_dept_id, 
       t1.count_dept, 
       t2.count_sub 
FROM   (SELECT dept_id, 
               sub_dept_id, 
               Count(dept_id) AS count_dept 
        FROM   employee_master 
        GROUP  BY dept_id)t1 
       LEFT JOIN (SELECT sub_dept_id, 
                         dept_id, 
                         Count(sub_dept_id) AS count_sub 
                  FROM   employee_master 
                  GROUP  BY sub_dept_id)t2 
              ON t1.dept_id = t2.dept_id