EDIT: Yogesh beat me to the MySQL 5 answer while I was AFK. You'll want to join your subquery on both the team_id
and the submission_file
in case you get multiple file submissions from a team on the same day.
Depending on what version of MySQL you are using, this can be done different ways.
SETUP
CREATE TABLE t1 (ID int, team_id int, submission_file varchar(30), the_date date) ;
INSERT INTO t1 (ID,team_id,submission_file,the_date)
SELECT 1, 1756, 'final_project.c', '2018-06-20 19:00:00' UNION ALL
SELECT 2, 1923, 'asdf.c', '2018-06-22 16:00:00' UNION ALL /**/
SELECT 3, 1756, 'untitled.c', '2018-06-21 20:00:00' UNION ALL /**/
SELECT 4, 1923, 'my_project.c', '2018-06-21 14:00:00' UNION ALL /**/
SELECT 5, 1756, 'untitled.c', '2018-06-21 08:00:00' UNION ALL
SELECT 6, 1814, 'my_project.c', '2018-06-20 12:00:00' UNION ALL/**/
SELECT 7, 1756, 'final_project.c', '2018-06-21 19:00:00' UNION ALL
SELECT 8, 1756, 'final_project.c', '2018-06-22 00:00:00' /**/
;
QUERIES
If you are using MySQL 5.x or lower, then you'll want to use a correlated subquery with a LIMIT
on it to pull up just the rows you want.
/* MySQL <8 */
SELECT a.*
FROM t1 a
WHERE a.id = (
SELECT b.id
FROM t1 b
WHERE b.team_id = a.team_id
AND b.submission_file = a.submission_file
ORDER BY b.the_date DESC
LIMIT 1
) ;
ID | team_id | submission_file | the_date
-: | ------: | :-------------- | :---------
2 | 1923 | asdf.c | 2018-06-22
3 | 1756 | untitled.c | 2018-06-21
4 | 1923 | my_project.c | 2018-06-21
6 | 1814 | my_project.c | 2018-06-20
8 | 1756 | final_project.c | 2018-06-22
MySQL 8 added window functions (FINALLY), and this makes a problem like this MUCH easier to solve, and likely much more efficient, too. You can sort the rows you need with a ROW_NUMBER()
window function.
/* MySQL 8+ */
SELECT s1.ID, s1.team_id, s1.submission_file, s1.the_date
FROM (
SELECT ID, team_id, submission_file, the_date
, ROW_NUMBER() OVER (PARTITION BY team_id, submission_file ORDER BY the_date DESC) AS rn
FROM t1
) s1
WHERE rn = 1
;
ID | team_id | submission_file | the_date
-: | ------: | :-------------- | :---------
8 | 1756 | final_project.c | 2018-06-22
3 | 1756 | untitled.c | 2018-06-21
6 | 1814 | my_project.c | 2018-06-20
2 | 1923 | asdf.c | 2018-06-22
4 | 1923 | my_project.c | 2018-06-21
db<>fiddle here
NOTE: After re-reading the OP, the intent may be different than what I originally read. In my queries, my filtering will return the most recent of all unique submission_file
names that a team submitted. So if a team submitted 3 files, you will get all 3 of the most recent versions of those files. If you remove submission_file
from the 5 subquery and the 8 PARTITION BY
, it will return only the most recent single file a team submitted regardless of name.