1

I have the following table in Postgres:

         Column          |            Type             |                   Modifiers                    
-------------------------+-----------------------------+------------------------------------------------
 id                      | uuid                        | not null
 name                    | character varying(255)      | not null
 parent_organization_ids | uuid[]                      | not null default '{}'::uuid[]

The column parent_organization_ids contains an array with all the parent hierarchy for that organization, having first the root, and in the last item the parent of the current row (if exists).

I need to do a query to show the current row information, and then do a left join to show the parent name of the current row.

How can I do that left join?

Is there an alternative to do this?

1
  • side note : Postgre is wrong either Postgres or PostgreSQL Commented Apr 28, 2015 at 4:34

1 Answer 1

1

Solved doing the following:

SELECT org.name, parent.name
FROM organizations org
LEFT JOIN organizations parent on 
org.parent_organization_ids[array_upper(org.parent_organization_ids, 1)]=parent.id;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's the way to go. Alternatively consider normalizing your schema, so you can have referential integrity, unique constraints etc.: stackoverflow.com/questions/8016776/…

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.