【问题标题】:Weighted-Avg Access Query加权平均访问查询
【发布时间】:2015-05-19 04:21:12
【问题描述】:

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

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;

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

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

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

【问题讨论】:

  • 对不起,我误解了你的问题,所以我删除了我的答案。以后可以发帖。再说一遍,你的销售表是什么?此处的表格似乎已经汇总。

标签: sql ms-access calculated-columns weighted-average


【解决方案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;

【讨论】:

  • 效果很好,非常感谢!我想我不知道您可以在 Access 查询中使用 sub-SELECT 语句。谢谢,曼努埃尔。
猜你喜欢
  • 2019-08-02
  • 2017-08-05
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
相关资源
最近更新 更多