I have 3 tables joined and I want to write a query which returns some fields from these joined tables.
There is a condition which I need to check against, and that is the number of rows specific to one user only.
To be more precise and clear, my tables look like this :
tblTest
------------------------------
TestID | StudentID | TestType
------------------------------
tblTestType
---------------------------
TestType | TestName
---------------------------
tblStudents
-------------------------------
StudentID | StudentName | Sex
-------------------------------
I am trying to get the list of students who have succeeded an exam in the first time (thus ‘tblTest’ resulting row count should be equal to 1 to indicate the fact that the user just took an exam and passed it, any number more than that means the student has taken that exam more than once),
and my query looks like this:
SELECT tblStudents.StudentName, COUNT(tblTest.StudentID) AS [Number of Times Exame is Taken], tblTest.TestID
FROM tblTest INNER JOIN
tblTestType ON tblTest.TestID = tblTestType.TestType INNER JOIN
tblStudents ON tblTest.StudentID =tblStudents.StudentID
GROUP BY tblTest.StudentID, tblTest.TestID
HAVING (tblTest.TestID = 1)
Which fails. Can someone please show me the correct way of doing this?
1 Answer