跨多个表聚合 SQL 查询
Aggregate SQL query across multiple tables
我正在使用 MS Access 2010。该数据库有一组 12 个相同的设备 table,一个公司的 12 个不同部门中的每一个。 table 跟踪需要由负责设备的人员(资源)采取的操作。我有一个简单的查询,用于计算具有各种状态的资源的数量。看起来如下:
SELECT dept1.actions.resource, dept1.action.status, Count(*) AS status_count
FROM dept1.action
GROUP BY dept1.action.status, dept1.action.resource;
每个 table 看起来像这样:
equip_id, text
resource, number (id of the resource who is responsible for the equipment)
status, number (id of the status of the action that the resource needs to do)
查询结果如下:
resource status status_count
1 1 63
2 1 79
5 1 16
6 1 3
0 3 1
1 3 1180
2 3 64
3 3 61
5 3 1
6 3 2
7 3 12
0 4 4
比如第一行显示资源1有63件状态为1的设备,第二行显示资源2有79件状态为1的设备……以此类推。
我需要的是一个聚合查询,它为所有资源和状态组合提供公司级别的总计,即完全相同的结果 table,除了 status_count 列将具有明显更大的数字。
感谢您提供的任何帮助。
基本上,您可以 select 计算数据库中每个部门 table 的计数,将它们合并,然后求和。
SELECT resource,status,sum(status_count) as status_company_count from(
SELECT dept1.actions.resource, dept1.action.status, Count(*) AS status_count
FROM dept1.action
GROUP BY dept1.action.status, dept1.action.resource
UNION
SELECT dept2.actions.resource, dept2.action.status, Count(*) AS status_count
FROM dept2.action
GROUP BY dept2.action.status, dept2.action.resource
UNION
SELECT dept3.actions.resource, dept3.action.status, Count(*) AS status_count
FROM dept3.action
GROUP BY dept3.action.status, dept3.action.resource
.....)
group by resource,status;
如果您能够更改架构,则将 12 个 table 合并为一个 table,并为 departmentid 添加一个额外的列。如果您不能更改数据库结构,也许您可以添加一个新的 table。然后你可以添加一个临时 table 来捕获每个 table 的结果到这个临时结果 table SELECT...INTO.
我正在使用 MS Access 2010。该数据库有一组 12 个相同的设备 table,一个公司的 12 个不同部门中的每一个。 table 跟踪需要由负责设备的人员(资源)采取的操作。我有一个简单的查询,用于计算具有各种状态的资源的数量。看起来如下:
SELECT dept1.actions.resource, dept1.action.status, Count(*) AS status_count
FROM dept1.action
GROUP BY dept1.action.status, dept1.action.resource;
每个 table 看起来像这样:
equip_id, text
resource, number (id of the resource who is responsible for the equipment)
status, number (id of the status of the action that the resource needs to do)
查询结果如下:
resource status status_count
1 1 63
2 1 79
5 1 16
6 1 3
0 3 1
1 3 1180
2 3 64
3 3 61
5 3 1
6 3 2
7 3 12
0 4 4
比如第一行显示资源1有63件状态为1的设备,第二行显示资源2有79件状态为1的设备……以此类推。
我需要的是一个聚合查询,它为所有资源和状态组合提供公司级别的总计,即完全相同的结果 table,除了 status_count 列将具有明显更大的数字。
感谢您提供的任何帮助。
基本上,您可以 select 计算数据库中每个部门 table 的计数,将它们合并,然后求和。
SELECT resource,status,sum(status_count) as status_company_count from(
SELECT dept1.actions.resource, dept1.action.status, Count(*) AS status_count
FROM dept1.action
GROUP BY dept1.action.status, dept1.action.resource
UNION
SELECT dept2.actions.resource, dept2.action.status, Count(*) AS status_count
FROM dept2.action
GROUP BY dept2.action.status, dept2.action.resource
UNION
SELECT dept3.actions.resource, dept3.action.status, Count(*) AS status_count
FROM dept3.action
GROUP BY dept3.action.status, dept3.action.resource
.....)
group by resource,status;
如果您能够更改架构,则将 12 个 table 合并为一个 table,并为 departmentid 添加一个额外的列。如果您不能更改数据库结构,也许您可以添加一个新的 table。然后你可以添加一个临时 table 来捕获每个 table 的结果到这个临时结果 table SELECT...INTO.