I am getting three different results in the following queries, they all have same joins & column to count just a condition to count is different, is there any way so that I could get the Questions, Correct, Attempted & IeslTopics.Name columns together in a single query?
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
You can use a conditional inside a
SUMto supply ones for items that you are looking for and zeros for other items, like this: