Order by date ASC, but DESC within group
I have two tables. Imagine the first one to be a directory, containing lot
of files (second table).
The second table (files) is containing a modification_date.
Now, I want to select ALL Directories and sort them by their modification
date ASC (so, the latest modification topmost). Instead of displaying that
folder, I just want to display the OLDEST Mofified file (so, modification
DESC, Group by folder_id)
Sorting ALL Files by their modification date is no problem.
My Query looks (simplified) like this:
SELECT
f.*,
d.*
FROM
files f
LEFT JOIN
directories d
ON
f.directory_id = d.id
ORDER BY
f.modification_date DESC
This gives me ALL files in their modification order (newest topmost) -
Now, I want to Group files within a folder, to only see the OLDEST
modification (they have "seen" Attributes, but taking that into account is
no big deal, so once a modification has been seen, the second oldest will
be displayed, etc...)
How can I sort a result by modification_date DESC, but also sort it by
modification_date ASC after Grouping it?
Example:
directories:
id | name
1 Folder 1
2 Folder 2
files
id | Name | d_id | modification_datee
1 f1 1 2008-01-01
2 f2 1 2011-01-01
3 f3 2 2013-01-01
4 f4 2 2010-01-01
Result I'd like to have:
f4 (cause directory 2 contains the NEWEST modification (2013), but f4 is
the oldest out of that folder)
f1 (cause directory 1 contains the SECOND newest modification, but f1 is
the oldest out of that folder)
Any suggestions?
No comments:
Post a Comment