2021-11-26

postgresql: query two tables with same column names and show the result side by side ordered their column names, which occur in both tables

Having two tables (table1, table2) with the same column names (generation, parent), the desired output would be the combination of all columns of both tables. Thereby the rows of table2 should join table1 so that the rows of table2 are matching those of table1 on generation column. The parent number should be ordered ascending for the entries in table1 as well as in table2. The number of rows of the query results should be equal of those of table1.

Given the following tables
table1:

| generation | parent |
|:----------:|:------:|
| 0          | 1      |
| 0          | 2      |
| 0          | 3      |
| 1          | 3      |
| 1          | 2      |
| 1          | 1      |
| 2          | 2      |
| 2          | 1      |
| 2          | 3      |

table2:

| generation | parent |
|:----------:|:------:|
| 1          | 3      |
| 1          | 1      |
| 1          | 3      |
| 2          | 1      |
| 2          | 2      |
| 2          | 3      |

The following queries are thought for creating and populating two sample tables as shown above:

create table table1(generation integer, parent integer);
insert into table1 (generation, parent) values(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(2,2),(2,1),(2,3);
create table table2(generation integer, parent integer);
insert into table2 (generation, parent) values(1,3),(1,1),(1,3),(2,1),(2,2),(2,3);

the imagined query should lead to the following desired result:

| table1_generation | table1_parent | table2_generation | table2_parent |
|:-----------------:|:-------------:|:-----------------:|:-------------:|
| 0                 | 1             |                   |               |
| 0                 | 2             |                   |               |
| 0                 | 3             |                   |               |
| 1                 | 1             | 1                 | 1             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 3             | 1                 | 3             |
| 2                 | 1             | 2                 | 1             |
| 2                 | 2             | 2                 | 2             |
| 2                 | 3             | 2                 | 3             |

Current query looks as follows:

with 
  p as (
    select 
      generation,
      parent 
    from 
      table1
    order by
      generation,
      parent
  ), o as(
    select
      generation,
      parent 
    from 
      table2 
    order by
      generation,
      parent
  )
select
  p.generation as table1_generation,
  p.parent as table1_parent,
  o.generation as table2_generation,
  o.parent as table2_parent
from
  p
left join o on 
  o.generation=p.generation;

Which leads to the following result:

| table1_generation | table1_parent | table2_generation | table2_parent |
|:-----------------:|:-------------:|:-----------------:|:-------------:|
| 0                 | 1             |                   |               |
| 0                 | 2             |                   |               |
| 0                 | 3             |                   |               |
| 1                 | 1             | 1                 | 1             |
| 1                 | 1             | 1                 | 3             |
| 1                 | 1             | 1                 | 3             |
| 1                 | 2             | 1                 | 1             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 3             | 1                 | 1             |
| 1                 | 3             | 1                 | 3             |
| 1                 | 3             | 1                 | 3             |
| 2                 | 1             | 2                 | 1             |
| 2                 | 1             | 2                 | 2             |
| 2                 | 1             | 2                 | 3             |
| 2                 | 2             | 2                 | 1             |
| 2                 | 2             | 2                 | 2             |
| 2                 | 2             | 2                 | 3             |
| 2                 | 3             | 2                 | 1             |
| 2                 | 3             | 2                 | 2             |
| 2                 | 3             | 2                 | 3             |

This link led to the conclusion, that any join command might not what is necessary here ... But union does only append rows... so for me it is absolutely unclear, how the desired result can be achieved o.O
Any help is highly appreciated. Thanks in advance!



from Recent Questions - Stack Overflow https://ift.tt/3nIHA9a
https://ift.tt/eA8V8J

No comments:

Post a Comment