0

背景:我有下表,其中包含服装品牌名称、销售品牌名称的数量、品牌名称带来的收入以及该品牌名称的每单位销售平均值。

Quantity Brand       Revenue       Rev_Per_Brand
1820     CMD         $13,519.50    $7.43
791      ATL         $8,997.00      $11.37
335      WHBM        $4,988.00      $14.89
320      CH          $4,593.50     $14.35
233      AT          $3,207.50     $13.77

目标:按品牌销量计算加权平均收入。

我现在拥有的:

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;

问题:我在表达式错误中收到不能有聚合函数,我猜它可能在SUM(COUNT(*))上面的部分。

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

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

4

1 回答 1

1

你不能加倍聚合,即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;
于 2015-05-20T16:49:21.413 回答