简体   繁体   English

如何在 Access 中按 % 划分组/数据?

[英]How do i divide group/data by % in Access?

This is the current code i have, it divides users by their "FWB" Score into distinct categories, just coping, doing great, getting by etc..这是我当前的代码,它根据用户的“FWB”分数将用户划分为不同的类别,只是应对、表现出色、过关等。

SELECT FWBScore, gender,respondentname,
IIf(FWBScore<=22.5,"Having Trouble",IIf(FWBScore>=25 AND FWBScore<=47.5,"Just Coping",IIf(FWBScore>=50. AND FWBScore<=75.0,"Getting By",IIf(FWBScore>=77.5,"Doing Great","Doing Great")))) AS Categories
FROM RESPONDENT;

Now instead of dividing them by their FWB score, i want to divide them by the % of people who responded in each category for example, the end result would look something like this现在,我不是将它们除以他们的 FWB 分数,而是将它们除以每个类别中做出响应的人的百分比,例如,最终结果看起来像这样

Doing Great  Getting by   Just Coping
24%           15%            10%

Any Help with this would be greatly appreciate.对此的任何帮助将不胜感激。

I would suggest just using a subquery and aggregating.我建议只使用子查询和聚合。 I suspect you want:我怀疑你想要:

SELECT AVG(IIF(Category = "Having Trouble", 100.0, 0)) as having_trouble,
       AVG(IIF(Category = "Just Coping", 100.0, 0)) as just_coping,
       AVG(IIF(Category = "Getting By", 100.0, 0)) as getting_by,
       AVG(IIF(Category = "Doing Great", 100.0, 0)) as doing_great      
FROM (SELECT FWBScore, gender, respondentname,
             IIf(FWBScore <= 22.5, "Having Trouble",
                 IIf(FWBScore >= 25 AND FWBScore <= 47.5, "Just Coping",
                     IIf(FWBScore >= 50. AND FWBScore <= 75.0, "Getting By",
                         IIf(FWBScore >= 77.5, "Doing Great", "Doing Great")
                        )
                    )
                ) AS Category
      FROM RESPONDENT
     ) as r;

First note.第一个注意。 You can simplify all those iif() conditions by using switch() .您可以使用switch()简化所有这些iif()条件。

Second note.第二个注意事项。 Strictly speaking, the subquery is not necessary.严格来说,子查询是没有必要的。 You can move the conditions to the outer query.您可以将条件移动到外部查询。 I used the subquery so I could leave your original logic.我使用了子查询,所以我可以保留你原来的逻辑。

EDIT:编辑:

Using switch() :使用switch()

FROM (SELECT FWBScore, gender, respondentname,
             SWITCH(FWBScore <= 22.5, "Having Trouble",
                    FWBScore >= 25 AND FWBScore <= 47.5, "Just Coping",
                    FWBScore >= 50. AND FWBScore <= 75.0, "Getting By",
                    FWBScore >= 77.5, "Doing Great", "Doing Great"
                   ) AS Category
      FROM RESPONDENT
     ) as r;

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

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