按月划分的类别份额百分比,而非累计

时间:2018-12-19 08:54:52

标签: ms-access ms-access-2013

我有一个数据,要计算类别的百分比份额(“文本”列),但是我想从计算中排除空白,并要按比例计算“月份”(文​​本列)

我的查询:

SELECT Category, Count(Category) AS CountOfCategory, Count([Category])/DCount("*","[Tablename]") AS MyPercent
FROM [Tablename]
GROUP BY Category;

查询的当前示例结果如下所示。 Access Query outcome

我的桌子看起来像这样

| Month Text | Category |  
|------------|----------|  
| Dec'18     | A        |  
| Nov'18     | A        |  
| Dec'18     | A        |  
| Nov'18     | C        |  
| Dec'18     | A        |  
| Nov'18     | C        |  
| Dec'18     | B        |  
| Nov'18     | C        |  

请帮助。

1 个答案:

答案 0 :(得分:0)

您的查询与您的数据示例不匹配,但我想此查询应该有帮助:

SELECT a.[Month Text], a.Category, Count(a.Category),
Count(a.Category)/
    (
    SELECT Count(b.Category)
    FROM yourtablename AS b
    WHERE b.[Month Text] = a.[Month Text]
    )
FROM yourtablename AS a
WHERE a.Category Is Not Null 
AND a.Category<>""
GROUP BY a.[Month Text], a.Category;