私はSQLの専門家ではないので、次の場合にSQLクエリを作成するための解決策を見つけるのに苦労しています。誰かがこれを手伝ってくれることを願っています!
Answerテーブルに20のQuestionsテーブルの回答が含まれているテーブルがいくつかあります。答えは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
---------------------------------
どんな助けでも大歓迎です!
ありがとうアレン