Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:19:31+00:00 2026-06-15T20:19:31+00:00

I have this query using PostgreSQL 9.1 (9.2 as soon as our hosting platform

  • 0

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..)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:19:32+00:00Added an answer on June 15, 2026 at 8:19 pm

    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:

    SELECT
        mf.album,
        mf.artist,
        ARRAY_AGG (mf.id) AS media_file_ids
    FROM
        "media_files" mf
    INNER JOIN "playlist_media_files" pmf ON mf.id = pmf.media_file_id
    WHERE
        pmf.playlist_id = 1
    GROUP BY
        mf.album,
        mf.artist
    ORDER BY
        mf.album ASC
    

    Now you can either use a subquery in the SELECT list or maybe use DISTINCT ON, though it looks like any solution based on DISTINCT ON will be so convoluted as not to be worth it.

    What you really want is something like an pick_arbitrary_value_agg aggregate 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 use min(artwork) or max(artwork) and you may find that this actually performs better than the later solutions.

    To use a subquery, leave the ORDER BY as it is and add the following as an extra column in your SELECT list:

    (SELECT mf2.artwork 
     FROM media_files mf2 
     WHERE mf2.artist = mf.artist
       AND mf2.album = mf.album
     LIMIT 1) AS picked_artwork
    

    You can at a performance cost randomize the selected artwork by adding ORDER BY random() before the LIMIT 1 above.

    Alternately, here’s a quick and dirty way to implement selection of a random row in-line:

    (array_agg(artwork))[width_bucket(random(),0,1,count(artwork)::integer)] 
    

    Since there’s no sample data I can’t test these modifications. Let me know if there’s an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that inserts rows, using a subquery like so: INSERT INTO
I have this issue: first, I execute a SQL query using Java and then
Looking to do a bit of refactoring... Using NHibernate I have this query currently
I have a strange problem. I do this query in a node.js server using
I have problem with my query on C, I’m using the oci8 driver. This
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this query... SELECT Distinct([TargetAttributeID]) FROM (SELECT distinct att1.intAttributeID as [TargetAttributeID] FROM AST_tblAttributes
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
I have this query SELECT zip, ( 3959 * acos( cos( radians(34.12520) ) *
In PostgreSQL, there is this very useful string_agg function, that allows query like: SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.