I have this query using PostgreSQL 9.1 (9.2 as soon as our hosting platform upgrades):
SELECT
media_files.album,
media_files.artist,
ARRAY_AGG (media_files. ID) AS media_file_ids
FROM
media_files
INNER JOIN playlist_media_files ON media_files.id = playlist_media_files.media_file_id
WHERE
playlist_media_files.playlist_id = 1
GROUP BY
media_files.album,
media_files.artist
ORDER BY
media_files.album ASC
and it’s working fine, the goal was to extract album/artist combinations and in the result set have an array of media files ids for that particular combo.
The problem is that I have another column in media files, which is artwork.
artwork is unique for each media file (even in the same album) but in the result set I need to return just the first of the set.
So, for an album that has 10 media files, I also have 10 corresponding artworks, but I would like just to return the first (or a random picked one for that collection).
Is that possible to do with only SQL/Window Functions (first_value over..)?
Yes, it’s possible. First, let’s tweak your query by adding alias and explicit column qualifiers so it’s clear what comes from where – assuming I’ve guessed correctly, since I can’t be sure without table definitions:
Now you can either use a subquery in the
SELECTlist or maybe useDISTINCT ON, though it looks like any solution based onDISTINCT ONwill be so convoluted as not to be worth it.What you really want is something like an
pick_arbitrary_value_aggaggregate that just picks the first value it sees and throws the rest away. There is no such aggregate and it isn’t really worth implementing it for the job. You could usemin(artwork)ormax(artwork)and you may find that this actually performs better than the later solutions.To use a subquery, leave the
ORDER BYas it is and add the following as an extra column in yourSELECTlist:You can at a performance cost randomize the selected artwork by adding
ORDER BY random()before theLIMIT 1above.Alternately, here’s a quick and dirty way to implement selection of a random row in-line:
Since there’s no sample data I can’t test these modifications. Let me know if there’s an issue.