3

我在以下查询中得到三个不同的结果,它们都有相同的连接和列来计算只是一个条件来计算是不同的,有什么办法可以让我得到 Questions, Correct, Attempted & 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为您要查找的项目提供条件,为其他项目提供零,如下所示:

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