0

我不是 SQL 专家,我很难找到一种解决方案来针对以下情况进行 SQL 查询。我希望有人可以帮助我解决这个问题!

我有几个表,其中 Answer 表包含 20 个问题表中的答案。答案的值可以是 1 到 5。

这些问题有一个 types_id,它将标记相关的问题。

我需要的是对答案表的查询并获得以下信息:

将相关的问题分组(= same types_id = same teamid and = same date)并从具有相同 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
---------------------------------

任何帮助将不胜感激!

谢谢阿伦

4

1 回答 1

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

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

于 2012-12-15T16:59:03.470 回答