我不是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
---------------------------------
任何帮助将不胜感激!
谢谢阿伦
如果您的答案表中没有每个问题20个列,而是20个行,并且有一个question_id列,这会更容易。 – Laurence
您使用的是什么RDBMS? – Taryn
首先使表格正常化。 – Kermit