汇总单个 SQL 查询中的多表计数

问题描述

我有三张表,详细信息如下:@H_404_1@

表 1:工作日志@H_404_1@

+-----------+------------+-------------+
| worklogid | technician | description |
+-----------+------------+-------------+
| 1         | john       | some text   |
+-----------+------------+-------------+
| 2         | jack       | some text   |
+-----------+------------+-------------+
| 3         | john       | some text   |
+-----------+------------+-------------+
| 4         | jenifer    | some text   |
+-----------+------------+-------------+

表 2:任务@H_404_1@

+--------+-------+-------------+
| taskid | owner | description |
+--------+-------+-------------+
| 1      | john  | some text   |
+--------+-------+-------------+
| 2      | john  | some text   |
+--------+-------+-------------+
| 3      | john  | some text   |
+--------+-------+-------------+
| 4      | jack  | some text   |
+--------+-------+-------------+

表 3:请求@H_404_1@

+-----------+------------+-----------+-------------+
| requestid | technician | title     | description |
+-----------+------------+-----------+-------------+
| 1         | john       | some text | ...         |
+-----------+------------+-----------+-------------+
| 2         | Sara       | some text | ...         |
+-----------+------------+-----------+-------------+
| 3         | john       | some text | ...         |
+-----------+------------+-----------+-------------+
| 4         | jack       | some text | ...         |
+-----------+------------+-----------+-------------+

现在我需要对这个结果进行 sql 查询:@H_404_1@

+------------+------------------+---------------+------------------+
| technician | count(worklogid) | count(taskid) | count(requestid) |
+------------+------------------+---------------+------------------+
| john       | 2                | 3             | 2                |
+------------+------------------+---------------+------------------+
| jack       | 1                | 1             | 1                |
+------------+------------------+---------------+------------------+
| jenifer    | 1                | 0             | 0                |
+------------+------------------+---------------+------------------+
| Sara       | 0                | 0             | 1                |
+------------+------------------+---------------+------------------+

我该怎么办?@H_404_1@

解决方法

一种方法是只使用 union all 和聚合:

select techician,sum(is_workid),sum(is_taskid),sum(is_requestid)
from ((select technician,1 as is_workid,0 as is_taskid,0 as is_requestid
       from worklog
      ) union all
      (select owner,1,0
       from task
      ) union all
      (select technician,1
       from request
      )
     ) t
group by technician;

在 Postgres 中,您还可以在加入之前进行聚合:

select *
from (select technician,count(*) as num_workid
      from worklog
      group by technician
     ) w full join
     (select owner as technician,count(*) as num_task
      from task
      group by owner
     ) t
     using (technician) full join
     (select technician,count(*) as num_request
      from request
      group by technician
     ) w 
     using (technician);

对于 full join,我发现 usingon 子句更简单。但是所有表中的名称必须相同。