从相同列但不同条件计数

时间:2012-09-18 10:23:30

标签: sql sql-server sql-server-2008

我在以下查询中得到三个不同的结果,它们都有相同的连接和&只计算一个要计数的条件的列是不同的,是否有任何方法可以让我得到问题,正确,尝试& 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

您可以在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