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

时间:2013-10-10 19:53:27

标签: sql ms-access-2010

请原谅我对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中工作,所以这不起作用。我考虑过使用IIFSwitch语句,但似乎没有一种允许正确的比较类型。我还发现有人建议使用具有两个不同分组的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;

任何帮助都会非常感激,甚至只是建议我可以在其他地方查看哪些类型的技术有用 - 我现在只是在圈子里跑。

1 个答案:

答案 0 :(得分:0)

HAVING这里真的是解决方案 - 将您的查询更改为使用HAVING与&gt; 3,使用HAVING&lt; = 3添加另一个查询,然后UNION ALL添加结果