简体   繁体   中英

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. 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.

+---------------+---------+------+------+------+
| 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. 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. 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. I also found where someone suggested a From statement that had two different groupings - but Access spat out an error when I tried it.

The only marginal success I've had is with a HAVING (((Count(Allmills.industry_code))>3)) , but it just drops the problem industries completely.

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它们的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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