3

次のクエリで3つの異なる結果が得られます。これらはすべて同じ結合と列を持ち、カウントする条件が異なります。質問、修正、試行、およびIeslTopics.Name列を一緒に取得できる方法はありますか。単一のクエリ?

SELECT     COUNT(TestResults2.QuestionID) AS Questions, Topics.Name
FROM         TestResults 
INNER JOIN   TestResults2 ON TestResults.ID = TestResults2.TestResultID 
INNER JOIN   QuestionBank ON TestResults2.QuestionID = QuestionBank.ID 
INNER JOIN   Topics ON QuestionBank.TopicID = Topics.ID
WHERE        (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY   Topics.Name
//All topic

SELECT     COUNT(TestResults2.QuestionID) AS Correct, Topics.Name
FROM         TestResults 
INNER JOIN   TestResults2 ON TestResults.ID = TestResults2.TestResultID 
INNER JOIN   QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND 
                             TestResults2.Answer = QuestionBank.Answer 
INNER JOIN   Topics ON QuestionBank.TopicID = Topics.ID
WHERE        (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY   Topics.Name
//Correct topic

SELECT     COUNT(TestResults2.QuestionID) AS Attempted, Topics.Name
FROM         TestResults 
INNER JOIN   TestResults2 ON TestResults.ID = TestResults2.TestResultID 
INNER JOIN   QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND 
                             TestResults2.Answer <> '\0' 
INNER JOIN   Topics ON QuestionBank.TopicID = Topics.ID
WHERE        (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY   Topics.Name
//Attempted topic
4

2 に答える 2

2

3つのクエリを結合し、すべてのトピック名を選択するクエリにも結合します。

//次のように編集します:

Select t.Name, a.Questions, b.Correct,c.Attempted from Topics t Left Join (
SELECT     COUNT(TestResults2.QuestionID) AS Questions, Topics.Name
FROM         TestResults INNER JOIN
                      TestResults2 ON TestResults.ID = TestResults2.TestResultID INNER JOIN
                      QuestionBank ON TestResults2.QuestionID = QuestionBank.ID INNER JOIN
                      Topics ON QuestionBank.TopicID = Topics.ID
WHERE     (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY Topics.Name) a On t.Name=a.Name
Inner Join(
SELECT     COUNT(TestResults2.QuestionID) AS Correct, Topics.Name
FROM         TestResults INNER JOIN
                      TestResults2 ON TestResults.ID = TestResults2.TestResultID INNER JOIN
                      QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND 
                      TestResults2.Answer = QuestionBank.Answer INNER JOIN
                      Topics ON QuestionBank.TopicID = Topics.ID
WHERE     (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY Topics.Name) b On t.Name=b.Name
Inner Join(
SELECT     COUNT(TestResults2.QuestionID) AS Attempted, Topics.Name
FROM         TestResults INNER JOIN
                      TestResults2 ON TestResults.ID = TestResults2.TestResultID INNER JOIN
                      QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND 
                      TestResults2.Answer <> '\0' INNER JOIN
                      Topics ON QuestionBank.TopicID = Topics.ID
WHERE     (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY Topics.Name) c on c.Name=t.Name
于 2012-09-18T10:27:09.247 に答える
2

次のように、a内の条件を使用して、SUM探しているアイテムに1を指定し、他のアイテムに0を指定できます。

SELECT
    COUNT(TestResults2.QuestionID) AS Questions
,   SUM(CASE WHEN TestResults2.Answer = QuestionBank.Answer THEN 1 ELSE 0 END) AS Correct
,   SUM(CASE WHEN TestResults2.Answer <> '\0' THEN 1 ELSE 0 END) AS Attempted
,   Topics.Name
FROM         TestResults INNER JOIN
                  TestResults2 ON TestResults.ID = TestResults2.TestResultID INNER JOIN
                  QuestionBank ON TestResults2.QuestionID = QuestionBank.ID INNER JOIN
                  Topics ON QuestionBank.TopicID = Topics.ID
WHERE     (TestResults.StudentID = 1) AND (TestResults.ID = 46)
GROUP BY Topics.Name
于 2012-09-18T10:31:22.300 に答える