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 

学生回答:

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

学生响应:

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

选项表:

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 回答