加权平均访问查询

Weighted-Avg Access Query

背景:我有以下table服装品牌,品牌的销售数量,品牌带来的收入,以及该品牌名称的平均每单位销售量。

Quantity Brand       Revenue       Rev_Per_Brand
1820     CMD         ,519.50    .43
791      ATL         ,997.00      .37
335      WHBM        ,988.00      .89
320      CH          ,593.50     .35
233      AT          ,207.50     .77

Objective:计算各品牌销量加权平均收入

我现在拥有的:

SELECT 
COUNT(*) AS [Quantity], 
Sales.Description AS Brand, 
FORMAT(SUM(Sales.Amt),"Currency") AS Revenue, 
Format(SUM(Sales.Amt)/COUNT(*), "Currency") AS Rev_Per_Brand, 

SUM(Sales.Amt)*(COUNT(*)/SUM(COUNT(*))) AS [wAvg_Rev_Per_Brand]

FROM Sales
WHERE Sales.Date > DateAdd("m",-1,NOW())
AND "This query lists brands that have sold in the past 1 month from today's date and returns the revenue received from them" <> ""
GROUP BY Sales.Description
ORDER BY COUNT(*) DESC;

问题: 我收到 cannot have aggregate function in expression 错误,我猜它可能在上面的 SUM(COUNT(*)) 部分。

我只是想计算特定品牌在所有已售品牌总数(总和)中的数量。谁能告诉我我做错了什么?

谢谢你,我非常感谢提前提供的任何帮助。

你不能双重聚合,即 SUM(COUNT(*)) 你必须在单独的子查询中计算,

将您的查询更改为:

SELECT 
COUNT(*) AS [Quantity], 
Sales.Description AS Brand, 
FORMAT(SUM(Sales.Amt),"Currency") AS Revenue, 
Format(SUM(Sales.Amt)/COUNT(*), "Currency") AS Rev_Per_Brand, 

SUM(Sales.Amt)*(COUNT(*)/(SELECT Count(*) FROM sales)) AS [wAvg_Rev_Per_Brand]

FROM Sales
WHERE Sales.Date > DateAdd("m",-1,NOW())
AND "This query lists brands that have sold in the past 1 month from today's date and returns the revenue received from them" <> ""
GROUP BY Sales.Description
ORDER BY COUNT(*) DESC;