This is the current code i have, it divides users by their "FWB" Score into distinct categories, just coping, doing great, getting by etc..
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
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()
.
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()
:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.