好的,我正在努力做到以下几点。我有一张表有问题和答案。我想弄清楚每个问题每个答案我有多少答案。
Question Answer
-------- ------
What is your favorite color? Blue
What is your favorite color? Red
What is your favorite color? Blue
What is your age? 12
What is your age? 15
现在我想要的是输出结果(查询):
What is your favorite color? Blue 2
What is your favorite color? Red 1
What is your age? 12 1
What is your age? 15 1
OR
What is your favorite color? 2 1
What is your age? 1 1
在最后一部分......我会根据查询知道这些值的含义。
答案 0 :(得分:2)
SELECT t.Question,
t.Answer,
COUNT(*) AS 'Count'
FROM YourTable AS t
GROUP BY t.Question,
t.Answer
答案 1 :(得分:2)
第二个问题的答案涉及count(distinct)
:
select t.Question, count(distinct t.Answer) as NumAnswers,
count(*) as NumAnswered
from table t
group by t.Question;