0

I have a query that looks like this:

SELECT
    s.*,
    array(
        SELECT m.id
        FROM swarm_machine sm
        JOIN machine m
        ON sm.machine_id = m.id
        WHERE swarm_id = s.id
    ) as machines
FROM swarm s
WHERE group_id = 48

Instead of returning just the machine ids SELECT m.id, i would like to return all the columns in the row instead SELECT m.*. PostgreSQL doesn't seem to allow this, but I was hoping that the community could help be figure it out.

I can always do this with 2 queries, but if PostgreSQL can do it for me I'd rather avoid the round trip.

SQL Fiddle: http://sqlfiddle.com/#!17/d7481/1

1 Answer 1

2

Postgres allows it. Just refer to the record:

SELECT s.*,
       ARRAY(SELECT m
             FROM swarm_machine sm JOIN
                  machine m
                 ON sm.machine_id = m.id
             WHERE swarm_id = s.id
            ) as machines
FROM swarm s;

Here is a SQL Fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.