0

以下のクエリがあります。

SELECT sa.StudentId, GROUP_CONCAT( DISTINCT StudentAnswer
ORDER BY StudentAnswer
SEPARATOR ',' ) AS StudentAnswer
FROM Student st
INNER JOIN Student_Answer sa ON ( st.StudentId = sa.StudentId )
INNER JOIN Student_Response sr ON ( sa.StudentId = sr.StudentId )
INNER JOIN Question q ON ( sr.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
WHERE q.SessionId =27
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId

出力結果は次のとおりです。

ここに画像の説明を入力

今、私はより大きなクエリを書くことを計画しているので、これらすべてのテーブルをFROM

しかし、結果の問題はStudentAnswer列です。学生の重複した学生の回答が表示されます。ステートメントからいくつかのテーブルを削除してクエリをわずかに小さくしたかのように、これは正しくありませんFROM。正しい学生の回答が表示されます。

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

ここに画像の説明を入力

StudentAnswer私の質問は、最初のクエリの列の下で正しい学生の回答を得ることができるかどうかです。

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

学生:

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

完全なクエリ:

SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId, q.QuestionId,
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, StudentMark
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sr.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
WHERE q.SessionId =27
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId


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)
4

1 に答える 1

0

実際、クエリはすでに機能しています。この条件を追加するだけです

SELECT  sa.StudentId
    ,   StudentForename
    ,   StudentSurname
    ,   q.SessionId
    ,   q.QuestionId
    ,   QuestionNo
    ,   o.OptionType
    ,   GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR ',') AS Answer
    ,   r.ReplyType
    ,   GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer
    ,   ResponseTime
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 -- <<==lacking condition
        INNER JOIN Question q
            ON (sr.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 sa.StudentId , q.QuestionId;

または、サブクエリを使用して値を連結することもできます

SELECT  DISTINCT sa.StudentId
    ,   StudentForename
    ,   StudentSurname
    ,   q.SessionId
    ,   q.QuestionId
    ,   QuestionNo
    ,   o.OptionType
    ,   an.QuestionAnswer
    ,   r.ReplyType
    ,   sa.StudentAns
    ,   ResponseTime
FROM    Student st
        INNER JOIN 
        (
            SELECT  StudentID, QuestionID,
                    GROUP_CONCAT(StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') StudentAns
            FROM    Student_Answer
            GROUP   BY StudentID, QuestionID
        )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 (sr.QuestionId = q.QuestionId) 
        INNER JOIN 
        (
            SELECT  QuestionID, 
                    GROUP_CONCAT(DISTINCT Answer ORDER BY Answer) QuestionAnswer
            FROM    Answer
            GROUP   BY QuestionID
        ) 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
于 2013-02-08T07:43:41.610 に答える