[英]Count the filtered data from multiple tables in SQL
I have 2 tables config_location_workstation and asset_workstation where both have the columns floor and workstation_number.我有 2 个表 config_location_workstation 和asset_workstation,它们都有列 floor 和workstation_number。
I want a query to return something like this:我想要一个查询返回如下内容:
FLOOR|地板| SUM PRODUCTION WORKSTATION |
SUM 生产工作站 | SUM HEAD COUNT
总人数
This code below came from another source which I was able to pull up the sum but the numbers are incorrect because what I want to get is the number of all Production workstation.下面的代码来自另一个来源,我能够提取总和但数字不正确,因为我想要得到的是所有生产工作站的数量。
SELECT config_location_workstation1.floor,
(SELECT COUNT(config_location_workstation2.workstation_number)
FROM config_location_workstation AS config_location_workstation2
WHERE config_location_workstation2.floor = config_location_workstation1.floor) AS SUM_FLOOR,
(SELECT COUNT(asset_workstation2.workstation_number)
FROM asset_workstation AS asset_workstation2
WHERE asset_workstation2.floor = config_location_workstation1.floor) AS SUM_HEAD
FROM config_location_workstation AS config_location_workstation1
INNER JOIN asset_workstation AS asset_workstation1
ON (config_location_workstation1.workstation_number = asset_workstation1.workstation_number)
WHERE config_location_workstation1.workstation_name = 'NORTH PRODUCTION'
GROUP BY config_location_workstation1.floor
The problem is WHERE is not working on this code.问题是 WHERE 不适用于此代码。 The Workstation Column sum is invalid.
工作站列总和无效。 It is pulling up all the entries.
它正在拉起所有条目。 I only need to query all PRODUCTION workstations
我只需要查询所有 PRODUCTION 工作站
Here's the current output.这是当前的 output。
+-------+-------------+------+------+
| Floor | Head Count | Workstations|
+-------+-------------+------+------+
| 18TH | 696 | 576 |
| 19TH | 381 | 463 |
| 20TH | 380 | 760 |
+-------+-------------+------+------+
Expected output for all Production workstations所有生产工作站的预期 output
+-------+-------------+------+------+
| Floor | Head Count | Workstations|
+-------+-------------+------+------+
| 18TH | 696 | 497 |
| 19TH | 381 | 388 |
| 20TH | 380 | 659 |
+-------+-------------+------+------+
Probably you could try using a subquery可能您可以尝试使用子查询
select * from (
SELECT config_location_workstation1.floor,config_location_workstation1.workstation_name as z,
(SELECT COUNT(config_location_workstation2.workstation_number)
FROM config_location_workstation AS config_location_workstation2
WHERE config_location_workstation2.floor = config_location_workstation1.floor) AS SUM_FLOOR,
(SELECT COUNT(asset_workstation2.workstation_number)
FROM asset_workstation AS asset_workstation2
WHERE asset_workstation2.floor = config_location_workstation1.floor) AS SUM_HEAD
FROM config_location_workstation AS config_location_workstation1
INNER JOIN asset_workstation AS asset_workstation1
ON (config_location_workstation1.workstation_number = asset_workstation1.workstation_number)
GROUP BY config_location_workstation1.floor
) x
WHERE z = 'NORTH PRODUCTION'
UPDATE:更新:
select x.*,y.* from ( SELECT *, COUNT(workstation_name) as COUNT_FLOOR
FROM config_location_workstation WHERE workstation_name LIKE '%PRODUCTION%' GROUP BY floor)x
join
(SELECT *, COUNT(floor) as COUNT_USERS FROM asset_workstation GROUP BY floor) y on
x.workstation_number = y.workstation_number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.