简体   繁体   English

如何在MS Access 2007中嵌套这两个SQL查询?

[英]How can I nest these two SQL queries in MS Access 2007?

I have a table called baskets with these columns: 我有一个名为baskets的表,带有以下列:

  • basket (name of the basket) 篮子(篮子的名称)
  • colour (colour of the basket) 颜色(篮子的颜色)
  • apples (the number of apples in the basket) 苹果(篮子中的苹果数量)
  • bananas (the number of bananas in the basket) 香蕉(篮子中香蕉的数量)
  • oranges (the number of oranges in the basket) 橘子(篮子中的橘子数量)
  • pears (the number of pears in the basket) 梨(篮子里的梨数)
  • peaches (the number of peaches in the basket) 桃子(篮子里的桃子数)

With Query1, I determine the total number of fruit in each basket and I also include the colour of each basket: 使用Query1,可以确定每个购物篮中的水果总数,还可以包含每个购物篮的颜色:

SELECT basket, colour, apples+bananas+oranges+pears+peaches AS fruit
FROM baskets;

Query1 consists of three columns: Query1由三列组成:

  • basket
  • colour 颜色
  • fruit (total number of fruit in the basket) 水果(篮子中的水果总数)

With Query2, I determine the average number of fruits there are in all baskets of each colour by drawing the information from the result of Query1: 使用Query2,我可以通过从Query1的结果中提取信息来确定每种颜色的所有购物篮中平均有多少水果:

SELECT DISTINCT
        candidate.colour,
        candidate.fruit
            (SELECT AVG(fruit)
                 FROM Query1 AS average
                 WHERE average.colour = candidate.colour) AS fruit
    FROM Query1 AS candidate;

Query2 consists of two columns: Query2由两列组成:

  • colour 颜色
  • fruit 水果

Is it possible to nest these queries so that I may obtain the result of Query2 with only one query? 是否可以嵌套这些查询,以便仅使用一个查询即可获得Query2的结果?

Your help will be much appreciated. 您的帮助将不胜感激。 Thank you. 谢谢。

SELECT colour, AVG(apples+bananas+oranges+pears+peaches) AS fruit
FROM baskets
GROUP by colour;

If you want the total fruit by colour of basket you would do something like this: 如果要按篮子颜色选择水果总数,可以执行以下操作:

SELECT colour, SUM(apples+bananas+oranges+pears+peaches) AS totalfruit
FROM baskets
GROUP By colour

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

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