简体   繁体   English

MS ACCESS中的GROUP BY和SUMS

[英]GROUP BY and SUMS in MS ACCESS

I'm trying to get a report of how many article have been sold, especially which one was sold more, both in terms of numbers and price. 我正在尝试获取一份已售出多少商品的报告,尤其是在数量和价格方面,哪一件销量更高。

I'm trying the above query, thinking that using the [PRICE]*[total] in the group by expression, it could worked. 我正在尝试上述查询,以为在表达式中使用[PRICE]*[total]group by可以正常工作。 unluckily it does not. 不幸的是,事实并非如此。 I've try also to put the alias in the group by expression, but nothing more, it only says that I need to use a grouping expression for the column: [PRICE]*[total] which is what I thought I have done. 我也尝试过将别名按表达式放在分组中,但仅此而已,它仅表示我需要对列使用分组表达式: [PRICE]*[total] ,这是我认为已完成的工作。

SELECT TOP 20 ARTIC, Sum(TOTGIA) AS total, [PRICE]*[total] AS a
FROM Car
GROUP BY ARTIC, [PRICE]*[total]
ORDER BY Sum(TOTGIA) DESC;

anyone could lead me in the good direction? 任何人都可以引导我朝好的方向发展?

the error is: 错误是:

"You tried to execute a query that does not include the specified expression '[PRICE]*[total]' as part of an aggregate function."

the table is something like this: 该表是这样的:

|artic|totgia|price
+++++++++++++++++++
|aaa  |  1   | 10
|aaa  |  4   | 10
|bbb  |  1   | 200

I would like to have: 我想拥有:

|aaa| 5 | 50
|bbb| 1 | 200

so aaa is the first one for number of sells, but bbb is first for cash 因此, aaa是销售数量第一个,而bbb是现金销售第一个

The problem here is that you are trying to use the total alias in the select and in the group by. 这里的问题是,您试图在select和group by中使用总别名。 You do not have access to the alias at this time. 您目前无权访问别名。 Instead, you will either need to refer to the actual column values in place of total. 取而代之的是,您将需要引用实际的列值来代替总数。 In other cases, you can create a subselect and use the alias, but this does not apply to your query as it is written. 在其他情况下,您可以创建子选择并使用别名,但这不适用于您编写的查询。

SELECT TOP 20 ARTIC, Sum(TOTGIA) AS total, PRICE*Sum(TOTGIA) AS a
FROM Car
GROUP BY ARTIC, PRICE
ORDER BY Sum(TOTGIA) DESC;

If you have an article listed with several different prices, this query will return several rows. 如果您列出的商品有几种不同的价格,则此查询将返回几行。 So, this data: 因此,此数据:

 |artic|totgia|price +++++++++++++++++++ |aaa | 1 | 10 |aaa | 4 | 20 |bbb | 1 | 200 

Would return these results: 将返回以下结果:

 |aaa| 1 | 10 |aaa| 4 | 80 |bbb| 1 | 200 

This would happen because we have specifically told sql that we want the unique articles and prices as their own rows. 发生这种情况是因为我们特别告诉sql,我们希望将唯一的商品和价格作为自己的行。 However, this is probably a good thing because in the above scenario, you wouldn't want to return that aaa has a quantity of 5 with a value of 50, since the total value is 90. If this is a possible scenario for your data, you would make this query into a subselect and group all the data for the unique articles together. 但是,这可能是一件好事,因为在上述情况下,您不希望返回aaa的数量为5,其值为50,因为总值为90。如果这是您数据的可能情况,您可以将此查询分为子查询,并将唯一文章的所有数据分组在一起。

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

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