-1

我有一个有 320 列的表。每列可能包含五个字母之一 (a,b,c,d,e) - 多项选择测试。现在我想进行一个统计分析,记住如果 10 个人中有 9 个用“b”回答问题,那么“b”很可能是正确的。

如何以最有效的方式做到这一点?我曾考虑过带有 order by 和 limits 的视图,但这对 320 列有效吗?

4

2 回答 2

0

您需要对每一列进行数学运算。sql 用于对行进行计算。

获取每个问题的答案计数:

select * from
(select a as answer union select b union select c union select d union select e) answers

left join 
(select answer_to_q1 as answer, count(*) as q1 from table group by 1) q1 on q1.answer=answers.answer

left join 
(select answer_to_q2 as answer, count(*) as q2 from table group by 1) q2 on q2.answer=answers.answer

... repeat for all columns

要获得最高计数的答案,其中 q1、q2 等是包含您对 q1 q2 的答案的列。

select 1 as question, q1 from table group by q1 order by count(*) desc limit 1
union all
select 2, q2 from table group by q1 order by count(*) desc  limit 1
....
于 2013-10-08T12:59:10.610 回答
0

您的架构远非最佳。

如果你标准化你的数据结构,你会发现它要容易得多。制作一张名为 answer 的表格:

create table answer (
    questionnaire_id int, -- an id number for which questionnaire this is 
    question_id int,      -- the id number of the question
    answer enum('a','b','c','d','e') -- the value of the answer 
);

然后,您可以使用如下查询查看每个问题的分布:

select question_id, answer,count(*)
from answer
group by question_id, answer; -- just one example of how to look at the answers
于 2013-10-08T12:59:34.377 回答