0

以下にクエリがあります。質問はStudentMarks、特定のStudentIdとQuestionIdに属するすべてをまとめてカウントし、それを表示するにはどうすればよいですか。

例えば:

Student_Answerテーブル:

StudentAnswerId (PK auto)  QuestionId (Fk Question) StudentId (Fk Student)  StudentMarks    
1                          72                          39                          2
2                          73                          39                          2
3                          73                          39                          1
4                          73                          39                          0
5                          72                          40                          0
6                          73                          40                          0
7                          73                          40                          1
8                          73                          40                          2

次に、以下のクエリを実行すると、次のようになります。

SELECT
        sa.StudentId, q.QuestionId,
        GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, StudentMark
        FROM Student st
        INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
        INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId) AND sa.QuestionId = sr.QuestionId
        INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
        INNER JOIN Answer an ON q.QuestionId = an.QuestionId
    WHERE q.SessionId = 27
          GROUP BY sa.StudentId, q.QuestionId
          ORDER BY StudentAlias, q.SessionId, QuestionNo

これを表示します:

StudentId  QuestionId StudentAnswer  StudentMark
39         72         C              0
39         73         A,C,E          0
40         72         D              2
40         73         B,C,D          2

ただし、のカウントStudentMarkは正しくありません。次のようになります。

StudentId  QuestionId StudentAnswer  StudentMark
39         72         C              2
39         73         A,C,E          3
40         72         D              0
40         73         B,C,D          3

アップデート:

     SELECT
        sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId, 
        QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, 
        GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
        GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, 
        SUM(StudentMark) AS SumStudentMarks
        FROM Student st
        INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
        INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId) AND sa.QuestionId = sr.QuestionId
        INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
        INNER JOIN Answer an ON q.QuestionId = an.QuestionId
        LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
        LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
    GROUP BY StudentId,QuestionId

Student - Student_Answer (StudentId)
Student - Student_Response (StudentId)
Student_Answer - Question (QuestionId)
Student_Response - Question (QuestionId)
Question - Answer (QuestionId)
Question - Reply (ReplyId)
Question - Option_Table (OptionId)

以下はテーブルのリストです。

学生:

StudentId (PK auto)  StudentForename  StudentSurname 
39                   Luke             McFadzen
40                   Chris            Tucker 

Student_Answer:

StudentAnswerId (PK auto)  QuestionId (FK Question)  StudentAnswer  StudentId (FK student)
1                          72                         D             39
2                          73                         B             39
3                          73                         C             39
4                          73                         D             39
5                          72                         C             40
6                          73                         A             40
7                          73                         C             40
8                          73                         E             40

Student_Response:

StudentResponseId (PK auto)  QuestionId (FK Question) ResponseTime  StudentId (FK student)
1                            72                       00:00:05      39
2                            73                       00:00:15      39
3                            72                       00:00:09      40
4                            73                       00:00:26      40

質問:

QuestionId (PK auto)  QuestionNo  SessionId (FK Session) ReplyId (FK Reply) OptionId (FK Option)    
72                    1           23                     1                  3
73                    2           23                     2                  7

答え:

AnswerId (PK auto)    QuestionId (FK Question)      Answer  
1                          72                         C             
2                          73                         A             
3                          73                         C             
4                          73                         D             

返事:

ReplyId (PK auto)  ReplyType
1                  Single
2                  Multiple

Option_Table:

ReplyId (PK auto)  ReplyType
1                  A-C
2                  A-D
3                  A-E
4                  A-F
5                  A-G
6                  A-H
7                  A-I
8                  A-J
4

1 に答える 1

1

SUM(StudentMark)だけでなくStudentMark

于 2013-02-08T09:02:05.860 に答える