I’m trying to return the first created record, and all but the first created record, for all individual users given certain constraints. My SQL is broken, but I can’t for the life of me figure out where/why.
Object Model
I have 4 models of interest: User, Course, Question, and QuestionRecord.
Everytime a User answers a Question, a QuestionRecord is generated. QuestionRecord is a many-to-one with User, Course, and Question. (Is that cardinality correct? Each QuestionRecord has a foreign key to exactly one User, one Course, and one Question)
In Ruby:
class QuestionRecord < ActiveRecord::Base
belongs_to :user
belongs_to :course
belongs_to :question
…
end
Desired Outcome #1: First Record
What I’d like is to return the first question record for each user, given a course and a question. Put another way, if Jane, and Bill have answered the question twice, John hasn’t attempted it, and Pete has answered it 7 times, I want 3 records returned, the first answer attempt, by creation date, from Jane, Bill, and Pete.
I could do this in the code, but want it in SQL for efficiency.
This is what I have:
SELECT qr.id
FROM users u
INNER JOIN question_records qr
ON u.id =
(SELECT x.user_id
FROM question_records x
WHERE x.course_id = #{course.id}
AND x.question_id = #{question.id}
AND x.user_id = u.id
ORDER BY created_at ASC
LIMIT 1 )
This query just runs for 40 minutes or so, and doesn’t actually return anything. I ran it with a subset of 50 records, and had the weird issue that all the returned qr.id results were “1”
Desired Result #2: All but the first record
Now here, I want all records for all users except the first record. My thought was that, in essence, this is the same query, except that I want all records, and I want to offset the returned index by one:
SELECT qr.id
FROM users u
INNER JOIN question_records qr
ON u.id =
(SELECT x.user_id
FROM question_records x
WHERE x.course_id = #{course.id}
AND x.question_id = #{question.id}
AND x.user_id = u.id
ORDER BY created_at ASC
OFFSET 1 )
However, it’s obvious that if the first query doesn’t work, the second won’t.
Coda
Does anyone have any guidance on this query? It’d be nice to have it as efficient as possible, so I could index question_records on [course_id, question_id] or whatever it takes. I’m also assuming that I’m missing a possibility to use “GROUP BY x.user_id”, but wasn’t sure how to add it without adding more gum in the works.
For the record, the database we’re using is MySQL.
For #1, if your ID values are sequentially issued:
Then, for #2: