我不是SQL的专家,我很难找到针对以下情况进行SQL查询的解决方案。我希望有人可以帮我这个!
我有几张表,其中答案表包含20个问题表中的答案。答案的值可以是1到5。
这些问题有一个types_id,用于标记相关的问题。
我需要的是对答案表的查询并获取以下信息:
将相关的问题分组(=相同的types_id =相同的teamid和=相同的日期),并从具有相同types_id的答案中获取AVG。
所以结果可能是:
---------------------------------------------------------
| 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与types_is为1相关。因此有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 :(得分:1)
首先,您需要取消隐藏问题数据。如果您不准备以这种方式存储数据,我会为此创建视图。您需要将此扩展到所有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