我有一个格式如下的表:
STATE SURVEY_ANSWER
NC high
NC moderate
WA high
FL low
NC high
我正在寻找一个可以得到以下结果的查询:
STATE HIGH MODERATE LOW
NC 2 1 0
WA 1 0 0
FL 0 0 1
不幸的是,这些是我得到的结果:
STATE HIGH MODERATE LOW
NC 3 1 1
WA 3 1 1
FL 3 1 1
这是我正在使用的代码:
Select mytable.STATE,
(SELECT COUNT(*) FROM mytable WHERE mytable.survey_answer = 'low' and state = mytable.state) AS low,
(SELECT COUNT(*) FROM mytable WHERE mytable.survey_answer = 'moderate' and state = mytable.state) AS moderate,
(SELECT COUNT(*) FROM mytable WHERE mytable.survey_answer = 'high' and state = mytable.state) AS high,
FROM mytable
GROUP BY mytable.state;
虽然这个论坛和其他论坛非常有帮助,但我无法弄清楚我做错了什么。请注意:我使用的是 Access,因此 CASE WHEN 解决方案不起作用。谢谢你的任何建议。