简体   繁体   English

SQL-MS Access-通过查询有条件地聚合一些行

[英]SQL - MS Access - Aggregate some rows conditionally via query

Please forgive my woefully limited understanding of SQL, but I'm hoping someone can help me. 请原谅我对SQL的有限理解,但我希望有人能帮助我。 I need to alter a query written by someone else some time ago. 我需要更改一段时间前由其他人编写的查询。

The query displays consumption per industry for a variety of industries in a number of areas. 该查询显示许多区域中各个行业的每个行业的消耗量。 The table it spits out currently looks something like this: 它吐出的表当前看起来像这样:

+---------------+----------+---------+
| Economic area | Industry |   Total |
+---------------+----------+---------+
| Area1         |          |         |
|               | Ind1     |  459740 |
|               | Ind2     |   43000 |
|               | Ind3     |       0 |
|               | Total    |  502740 |
| Area2         |          |         |
|               | Ind1     |  725560 |
|               | Ind2     |  111017 |
|               | Ind3     |  277577 |
|               | Total    | 1114154 |
+---------------+----------+---------+

Unfortunately, this table in conjunction with another table we publish on the number of producers in each industry and area can reveal commercially sensitive information when there are very few producers. 不幸的是,此表与我们发布的有关每个行业和地区的生产商数量的另一个表可以在生产商很少的情况下揭示商业敏感信息。 For instance, in the table below, there's only one producer in Industry 2 in Area 1, so everything in the above table consumed by industry 2 in Area 1 goes to that producer. 例如,在下表中,区域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 |
+---------------+---------+------+------+------+

What I've been asked to do is to produce a condensed version of the first table that looks like the one below, where industries that have less than 3 producers in an area are aggregated into a generic Other Industry. 我被要求做的是生成第一个表格的精简版本,如下表所示,该区域中生产者少于3个的行业被汇总为一个通用的其他行业。 Something like this: 像这样:

+---------------+----------+--------+
| Economic area | Industry |  All   |
+---------------+----------+--------+
| Area1         |          |        |
|               | Ind1     | 459740 |
|               | OtherInd | 121376 |
|               | Total    | 581116 |
| Area2         |          |        |
|               | Ind1     | 725560 |
|               | Ind2     | 111017 |
|               | Ind3     |    244 |
|               | Total    | 836821 |
+---------------+----------+--------+

I have been searching for a while, but haven't been able to find anything that works, or that I can understand well enough to make it work. 我已经搜索了一段时间,但找不到任何有效的方法,或者我无法完全理解以使其起作用。 I tried using a Count(Case(industry_code<3,1,0)) ... but I'm working in MS Access, so that doesn't work. 我尝试使用Count(Case(industry_code<3,1,0)) ...但是我在MS Access中工作,所以不起作用。 I thought about using and IIF or a Switch statement, but it doesn't seem like either of those allow for the right type of comparison. 我考虑过使用IIFSwitch语句,但似乎两者都不支持正确的比较类型。 I also found where someone suggested a From statement that had two different groupings - but Access spat out an error when I tried it. 我还发现有人在其中建议具有两个不同分组的From语句-但是当我尝试它时Access吐出一个错误。

The only marginal success I've had is with a HAVING (((Count(Allmills.industry_code))>3)) , but it just drops the problem industries completely. 我获得的唯一微不足道的成功是HAVING (((Count(Allmills.industry_code))>3)) ,但这只是完全解决了问题行业。

Currently the a somewhat simplified version of the query looks like this: 当前,查询的简化版本如下所示:

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;

Any help would be greatly appreciated, even just suggestions of what types of techniques might be useful that I can look into elsewhere - I'm just running in circles right now. 我们将不胜感激任何帮助,甚至只是建议我可以在其他地方研究的哪种类型的技术可能有用的建议-我现在正处于圈子中。

HAVING确实是这里的解决方案-更改您的查询以使用HAVING > 3,添加另一个HAVING <= 3的查询,然后UNION ALL它们的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM