2012-12-15 149 views
0

我不是SQL專家,我很難找到解決方案來爲以下情況製作SQL查詢。我希望有人能幫助我這個!分組答案需要SQL查詢解決方案

我有幾個表的答案表持有20個問題表的答案。答案可以有1到5的值。

問題有一個types_id標記相關的問題。

我需要的是在答案表的查詢,並得到了以下信息:

集團所涉及的問題(=相同types_id =相同teamid和=同一日期),並從答案拿AVG是有相同的types_id。

所以結果可能是這樣的:

--------------------------------------------------------- 
|          types_id  | 
|teamid | date    | 1 | 2 | 3 | 4 | 
--------------------------------------------------------- 
| 12 | 2012-12-31 00:00:00 | 2 | 4 | 3 | 5 | <- holds the average answers from the related questions (= same types_id) 
--------------------------------------------------------- 

此處作爲一例的問題1,5,9,13和17通過那裏的1 types_is涉及因此,有4組的相關問題。

下表結構的一個樣本:

Answers表:

----------------------------------------------------------------------------------------------------------------------------------------------------- 
id teamid userid date     Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 timestamp   done 
----------------------------------------------------------------------------------------------------------------------------------------------------- 
1  12  1 2012-12-31 00:00:00  1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 2012-12-11 08:30:27 0 
2  12  2 2012-12-31 00:00:00  5 2 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2012-12-11 08:50:08 0 
3  12  3 2012-12-31 00:00:00  1 3 1 1 1 1 2 2 2 2 2 4 4 4 4 4 5 5 5 5 2012-12-11 08:20:37 0 
1  9  11 2012-12-31 00:00:00  1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 2012-12-11 08:30:27 0 
2  9  12 2012-12-31 00:00:00  5 2 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2012-12-11 08:50:08 0 
3  9  23 2012-12-31 00:00:00  1 3 1 1 1 1 2 2 2 2 2 4 4 4 4 4 5 5 5 5 2012-12-11 08:20:37 0 
    ----------------------------------------------------------------------------------------------------------------------------------------------------- 

Questions

--------------------------------- 
id question   types_id 
--------------------------------- 
1 Question 1 text  1 
2 Question 2 text  2 
3 Question 3 text  3 
4 Question 4 text  4 
5 Question 5 text  1 
6 Question 6 text  2 
7 Question 7 text  3 
8 Question 8 text  4 
9 Question 9 text  1 
10 Question 10 text  2 
11 Question 11 text  3 
12 Question 12 text  4 
13 Question 13 text  1 
14 Question 14 text  2 
15 Question 15 text  3 
16 Question 16 text  4 
17 Question 17 text  1 
18 Question 18 text  2 
19 Question 19 text  3 
20 Question 10 text  4 
--------------------------------- 

任何幫助將不勝感激!

謝謝阿倫

+0

如果您的答案表中沒有每個問題20個列,而是20個行,並且有一個question_id列,這會更容易。 – Laurence

+0

您使用的是什麼RDBMS? – Taryn

+0

首先使表格正常化。 – Kermit

回答

1

首先你需要unpivot的問題數據。如果您不準備以這種方式存儲數據,我會爲此創建視圖。你需要這個擴大到所有20個問題:

Create View UnpivotedAnswers As 
Select 
    teamid, 
    date, 
    1 as QuestionID, 
    Q1 as Answer 
From 
    Answers 
Union All 
Select 
    teamid, 
    date, 
    2 as QuestionID, 
    Q2 as Answer 
From 
    Answers 
Union All 
Select 
    teamid, 
    date, 
    5 as QuestionID, 
    Q5 as Answer 
From 
    Answers 

一旦你有這種格式提供的數據,得到平均數可以像這樣做:

Select 
    u.teamid, 
    u.date, 
    avg(case When q.types_id = 1 Then Answer End) as type1, 
    avg(case When q.types_id = 2 Then Answer End) as type2, 
    avg(case When q.types_id = 3 Then Answer End) as type3, 
    avg(case When q.types_id = 4 Then Answer End) as type4, 
    avg(case When q.types_id = 5 Then Answer End) as type5 
From 
    UnpivotedAnswers u 
    Inner Join 
    Questions q 
    On u.QuestionID = q.id 
Group By 
    u.teamid, 
    u.date 

http://sqlfiddle.com/#!2/b1b718/1

+0

非常感謝你! – Aren