0
鑑於其描述受訪者responsest到由問題(Q1,Q2..Qn),每個都具有選擇題答案(Q1a1,Q1a2,Q2a1 ...)的一項調查顯示如下表模式:這個總結調查數據的SQLite3查詢可以改進嗎?
respondent | group | Q1 | Q2 | ... Qn
fred | xyz | Q1a1 | Q2a3 | ...
joe | xyz | Q1a2 | q2a1 | ...
bill | abc | Q1a1 | Q2a4 | ...
在一個簡單的問題中,Q1a1可能是「是」,Q1a2可能是「否」,但可以認爲答案是任何簡單的文本字符串,它們在源數據中的存在是已知的(可能忽略可能但未使用的答案的可能性) 。
每一行代表一個獨特的受訪者,每個組別中都有很多受訪者。每個回答者回答每個問題,給出每個問題的有效答案之一。
我想製表,每組有多少(portentially無)的每個回答有針對每個問題:我想製作:
group | Q1_a1 | Q1_a2 | Q1_a3 | Q2_a1 | Q2_a2 | Q3_a1 | ...
xyz | 32 | 12 | 9 | 11 | 23 | 2 | ...
abc | 27 | 6 | | 18 | 12 | 26 | ...
我想這樣做在sqlite3的和我有一個查詢,我想工作,但我也認爲這是笨重和大量的問題,不能很好地擴展:
WITH Q1YES AS (SELECT RespondentGroup, COUNT(*) AS yes FROM DATA
WHERE Q1 = 'Yes'
GROUP BY RespondentGroup),
Q1NO AS (SELECT RespondentGroup, COUNT(*) AS no FROM DATA
WHERE Q1 = 'No'
GROUP BY RespondentGroup),
Q2YES AS (SELECT RespondentGroup, COUNT(*) AS yes FROM DATA
WHERE Q2 = 'Yes'
GROUP BY RespondentGroup),
Q2NO AS (SELECT RespondentGroup, COUNT(*) AS no FROM DATA
WHERE Q2 = 'No'
GROUP BY RespondentGroup),
Q2PART AS (SELECT RespondentGroup, COUNT(*) AS part FROM DATA
WHERE Q2 = 'Part'
GROUP BY RespondentGroup)
SELECT DISTINCT DATA.RespondentGroup as RespondentGroup,
Q1YES.yes as "Q1 Yes", Q1NO.no as "Q1 No",
Q2YES.yes as "Q2 Yes", Q2NO.no as "Q2 No", Q2PART.part as "Q2 Part"
FROM DATA
LEFT OUTER JOIN Q1YES on Q1YES.RespondentGroup = DATA.RespondentGroup
LEFT OUTER JOIN Q1NO on Q1NO.RespondentGroup = DATA.RespondentGroup
LEFT OUTER JOIN Q2YES on Q2YES.RespondentGroup = DATA.RespondentGroup
LEFT OUTER JOIN Q2NO on Q2NO.RespondentGroup = DATA.RespondentGroup
LEFT OUTER JOIN Q2PART on Q2PART.RespondentGroup = DATA.RespondentGroup
ORDER BY RespondentGroup;
可以這樣查詢得到改善(答案必須sqlite3的工作)?
在SQLite中,布爾表達式返回0/1,所以這可以用sum(Q1 ='Yes')等簡化。 –