In my PostgreSQL database I have table with such structure:
| organization_id | organization_name | parent_organization_id | tree_organization_id |
|-----------------|-------------------|------------------------|----------------------|
| 1 | Alphabet | | |
| 2 | Google | 1 | \1\2 |
| 3 | Google X | 2 | \1\2\3 |
As you can see, the table stores a hierarchical relationship between organizations. Let's say as input I have an array of ids. For simplicity, let's say I have the following array [3]
. How do I get a list of all parent organizations from an array? In my case, the final result which I want to see is this:
| organization_id | organization_name | parent_organization_id | tree_organization_id |
|-----------------|-------------------|------------------------|----------------------|
| 1 | Alphabet | | |
| 2 | Google | 1 | \1\2 |
Do I need to focus on the tree_organization_id
column or recursively iterate over the parent_organization_id
column?
PostgreSQL version:
PostgreSQL 11.4 (Debian 11.4-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
Columns types:
| Column name | Column type |
|------------------------|-------------|
| organization_id | int8 |
| organization_name | varchar |
| parent_organization_id | int8 |
| tree_organization_id | varchar |