2013-10-08 49 views
-1

我有一個320列的表。每列可以包含五個字母(a,b,c,d,e)中的一個 - 多選題測試。現在我想進行統計分析,因爲如果10個人中有9個人回答'b','b'的問題可能是正確的。在MySQL中執行統計分析的最佳方法

這怎麼能以最有效的方式完成?我曾經想過一個按順序排列的視圖,但是這對320列是有效的嗎?

+1

[數據庫規範化(http://en.wikipedia.org/wiki/Database_normalization) – Kermit

+0

表中有多少行?將整個事件複製到Excel等電子表格應用程序並在那裏執行分析可能會更好。如果記錄數量不是很大(小於50,000),則電子表格應用程序更適合在許多列上執行相同的操作。 – Dan

+0

我真的沒有看到downvote的原因... – Jan

回答

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 
.... 
0

你的模式遠非最佳。

如果你規範化你的數據結構,你會發現它更容易。做一個表被叫應答:

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 
相關問題