[英]SQL - MS Access - Aggregate some rows conditionally via query
请原谅我对SQL的有限理解,但我希望有人能帮助我。 我需要更改一段时间前由其他人编写的查询。
该查询显示许多区域中各个行业的每个行业的消耗量。 它吐出的表当前看起来像这样:
+---------------+----------+---------+
| Economic area | Industry | Total |
+---------------+----------+---------+
| Area1 | | |
| | Ind1 | 459740 |
| | Ind2 | 43000 |
| | Ind3 | 0 |
| | Total | 502740 |
| Area2 | | |
| | Ind1 | 725560 |
| | Ind2 | 111017 |
| | Ind3 | 277577 |
| | Total | 1114154 |
+---------------+----------+---------+
不幸的是,此表与我们发布的有关每个行业和地区的生产商数量的另一个表可以在生产商很少的情况下揭示商业敏感信息。 例如,在下表中,区域1的行业2中只有一个生产者,因此上表中区域1的行业2消耗的所有东西都归该生产者所有。
+---------------+---------+------+------+------+
| Economic area | County | Ind1 | Ind2 | Ind3 |
+---------------+---------+------+------+------+
| Area1 | | | | |
| | county1 | 1 | 0 | 0 |
| | county2 | 3 | 1 | 2 |
| | county3 | 1 | 0 | 0 |
| | Total: | 5 | 1 | 2 |
| | | | | |
| Area2 | county4 | 5 | 0 | 1 |
| | county5 | 3 | 3 | 1 |
| | county6 | 1 | 0 | 1 |
| | county7 | 0 | 0 | 0 |
| | Total: | 9 | 3 | 3 |
+---------------+---------+------+------+------+
我被要求做的是生成第一个表格的精简版本,如下表所示,该区域中生产者少于3个的行业被汇总为一个通用的其他行业。 像这样:
+---------------+----------+--------+
| Economic area | Industry | All |
+---------------+----------+--------+
| Area1 | | |
| | Ind1 | 459740 |
| | OtherInd | 121376 |
| | Total | 581116 |
| Area2 | | |
| | Ind1 | 725560 |
| | Ind2 | 111017 |
| | Ind3 | 244 |
| | Total | 836821 |
+---------------+----------+--------+
我已经搜索了一段时间,但找不到任何有效的方法,或者我无法完全理解以使其起作用。 我尝试使用Count(Case(industry_code<3,1,0))
...但是我在MS Access中工作,所以不起作用。 我考虑过使用IIF
或Switch
语句,但似乎两者都不支持正确的比较类型。 我还发现有人在其中建议具有两个不同分组的From语句-但是当我尝试它时Access吐出一个错误。
我获得的唯一微不足道的成功是HAVING (((Count(Allmills.industry_code))>3))
,但这只是完全解决了问题行业。
当前,查询的简化版本如下所示:
SELECT
economic_areas.economic_area AS [Economic area],
Industry_codes.industry_heading AS Industry,
Sum(Allmills.consumption) AS [All],
Sum(Allmills.[WA origin logs]) AS Washington
Allmills.industry_code,
Count(Allmills.industry_code) AS CountOfindustry_code,
Sum(Allmills.industry_code) AS SumOfindustry_code
FROM ((economic_areas INNER JOIN Allmills ON (economic_areas.state_abbrev =
Allmills.state_abbrev)
AND (economic_areas.economic_area_code = Allmills.economic_area_code))
INNER JOIN Industry_codes ON Allmills.display_industry_code =
Industry_codes.industry_code)
WHERE (((Allmills.economic_area_code) Is Not Null))
GROUP BY Allmills.display_industry_economic_area_code,
Allmills.display_industry_code, economic_areas.economic_area,
Industry_codes.industry_heading, Allmills.industry_code
ORDER BY Allmills.display_industry_economic_area_code,
Allmills.display_industry_code;
我们将不胜感激任何帮助,甚至只是建议我可以在其他地方研究的哪种类型的技术可能有用的建议-我现在正处于圈子中。
HAVING
确实是这里的解决方案-更改您的查询以使用HAVING
> 3,添加另一个HAVING
<= 3的查询,然后UNION ALL
它们的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.