Supermercado
Diamond Member
This is probably going to be quite lengthy and if you make it through, I applaud your patience. I?m working on a project using ASP.NET and SQL Server and I know just enough SQL to be dangerous, I think. I?ve been trying to come up with a query to do what is described below and what I?ve got is really really close but it?s not quite there and I?m having a hard time figuring out what I?m missing.
What I?m trying to do is build a custom quiz application. The end goal is for a user to come on, start taking a quiz, and then be graded and have the ability to retake it, taking only the questions they missed on the previous attempt(s). The database I?ve got designed handles all this just fine, I think, and is as described below. There are multiple levels and sections and subsections to this but all that?s really applicable is that a quiz is part of a subsection. I?ve only included (what I think are) the relevant fields in the tables.
Questions
ID
SubSectionID
CorrectAnswerID
SortNum
Attempts
ID
UserID
SubSectionID
AttemptsSub
ID
AttemptID
QuestionID
SelectedChoiceID
Users
ID
All attempts (time, date, user, and so forth) have to be recorded, as well as what answers were picked on what attempt. The idea is that a record of an attempt is kept in the Attempts table and the specifics of that attempt are in the AttemptsSub table with a foreign key back to the Attempts table. In the mix is also an Answers table but I don?t think it?s really applicable to the problem at hand.
What I?m trying to do is write a query that will return me the question ID and correct answer ID of the next question in the sequence for the user, so long as there is a next question.
The query I?ve got to this point is below and it?s very close but I?m getting some errors in what I?m getting back. I?m working on trying to debug what?s being returned but so far, it?s not making a whole lot of sense.
SELECT q.ID, q.CorrectAnswerID
FROM Questions AS q
INNER JOIN Attempts AS a ON q.SubSectionID = a.SubSectionID
INNER JOIN Users AS u ON u.ID = a.UserID
INNER JOIN AttemptsSub AS s ON s.AttemptID = a.ID
AND q.CorrectAnswerID <> s.SelectedChoiceID
WHERE (q.SubSectionID = @SubSectionID) AND (a.UserID = @UID)
ORDER BY SortNum ASC
@SubSectionID and @UID are parameters.
I hope that there?s enough information there for someone to give me a push in the right direction. It feels like it?s so close but just not quite there. Any help would be very much appreciated.
Edit: Just fixing a typo in the query here that's correct in the actual query.
Update:
I've narrowed down what's going on with what I'm getting back. I was playing with my test data and it looks like there's an increase in data each time I add a new bit of test data.
My test data is 5 questions, numbered 1-5. The first time I run the query with just q1 answered correctly, I get the expected result, which is why I thought the query was working. When I add a second bit of data to answer q2 correctly, I get 8 results instead of the expected 3. It's returning the correctly answered questions (1, 2) once and the incorrectly answered questions twice (3, 4, 5). If I do the same thing again and answer q3 correctly, I get 12 results, the correct questions (1, 2, 3) twice each and the incorrect questions (4, 5) three times each. Same again with q4, 16 rows, three times and four times.
What might cause something like that? I've never seen anything like this.
What I?m trying to do is build a custom quiz application. The end goal is for a user to come on, start taking a quiz, and then be graded and have the ability to retake it, taking only the questions they missed on the previous attempt(s). The database I?ve got designed handles all this just fine, I think, and is as described below. There are multiple levels and sections and subsections to this but all that?s really applicable is that a quiz is part of a subsection. I?ve only included (what I think are) the relevant fields in the tables.
Questions
ID
SubSectionID
CorrectAnswerID
SortNum
Attempts
ID
UserID
SubSectionID
AttemptsSub
ID
AttemptID
QuestionID
SelectedChoiceID
Users
ID
All attempts (time, date, user, and so forth) have to be recorded, as well as what answers were picked on what attempt. The idea is that a record of an attempt is kept in the Attempts table and the specifics of that attempt are in the AttemptsSub table with a foreign key back to the Attempts table. In the mix is also an Answers table but I don?t think it?s really applicable to the problem at hand.
What I?m trying to do is write a query that will return me the question ID and correct answer ID of the next question in the sequence for the user, so long as there is a next question.
The query I?ve got to this point is below and it?s very close but I?m getting some errors in what I?m getting back. I?m working on trying to debug what?s being returned but so far, it?s not making a whole lot of sense.
SELECT q.ID, q.CorrectAnswerID
FROM Questions AS q
INNER JOIN Attempts AS a ON q.SubSectionID = a.SubSectionID
INNER JOIN Users AS u ON u.ID = a.UserID
INNER JOIN AttemptsSub AS s ON s.AttemptID = a.ID
AND q.CorrectAnswerID <> s.SelectedChoiceID
WHERE (q.SubSectionID = @SubSectionID) AND (a.UserID = @UID)
ORDER BY SortNum ASC
@SubSectionID and @UID are parameters.
I hope that there?s enough information there for someone to give me a push in the right direction. It feels like it?s so close but just not quite there. Any help would be very much appreciated.
Edit: Just fixing a typo in the query here that's correct in the actual query.
Update:
I've narrowed down what's going on with what I'm getting back. I was playing with my test data and it looks like there's an increase in data each time I add a new bit of test data.
My test data is 5 questions, numbered 1-5. The first time I run the query with just q1 answered correctly, I get the expected result, which is why I thought the query was working. When I add a second bit of data to answer q2 correctly, I get 8 results instead of the expected 3. It's returning the correctly answered questions (1, 2) once and the incorrectly answered questions twice (3, 4, 5). If I do the same thing again and answer q3 correctly, I get 12 results, the correct questions (1, 2, 3) twice each and the incorrect questions (4, 5) three times each. Same again with q4, 16 rows, three times and four times.
What might cause something like that? I've never seen anything like this.