I have a table with the following schema:
CREATE TABLE IF NOT EXISTS `ATTEMPTS`
(
ID INT NOT NULL AUTO_INCREMENT,
USERS_ID INT,
TESTS_ID INT,
SCORE FLOAT(10, 4),
CREATED DATETIME DEFAULT NULL,
MODIFIED DATETIME DEFAULT NULL,
IS_DELETED BIT(1) DEFAULT NULL,
PRIMARY KEY(ID)
);
A user can have multiple attempts for different tests. I am trying to think through the best way to return all of the most recent test attempts for each user. So if user A has two attempts for test 8 and three for test 17, while user B has one attempt for test 8 then the returned table would have 3 records: two from user A and one from user B. These records being the respective most recent test attempts for each test type taken.
I think a self join is needed for this, but I'm not sure. It there a way around using a self join? I'm not sure what is the best solution to return what I want from this table. I cannot think of a way to filter by "most recent" given the organization of the data. Basically, I'm not sure how to build this query.
This is my (NOT YET WORKING) query thus far:
SELECT a.USER_ID,
a.TEST_ID,
a.SCORE,
a.MODIFIED
FROM ATTEMPTS AS a,
ATTEMPTS AS b
WHERE a.USER_ID = b.USER_ID
AND (Some clause to deal with most recent?)
Thank you to anyone who can help.