Skip to content Skip to sidebar Skip to footer

Join Solution On Postgres With Many Tables

Little issue on a Postgres database, being managed in Acqua Data Studio: I need a solution to create a SELECT that concatenates many views into one table. There are more than 10 vi

Solution 1:

SQL Fiddle

select central, id, map, camel, nrrg
from
    v1
    fullouterjoin
    v2 using (central, id)
    fullouterjoin
    v3 using (central, id)
orderby central, id
;
 central | id | map  | camel | nrrg  
---------+----+------+-------+-------
 A       |1| MAP1 | CAP1  | NRRG2
 A       |2| MAP1 || 
 A       |3||| 
 B       |1| MAP3 | CAP1  | 
 B       |2|| CAP2  | 
 B       |3|| CAP3  | 
 C       |1||| 
 D       |1||| 
 D       |5||| NRRG1

Solution 2:

A full outer join is extra complicated when you have composite keys. Instead, use the union all/group by method:

select central, id, max(map) asmap, max(camel) as camel, max(nrrg) as nrrg
from ((select central, id, map, null as camel, null as nrrg
       from view1
      ) union all
      (select central, id, null asmap, camel, null as nrrg
       from view2
      ) union all
      (select central, id, null asmap, null as camel, nrrg
       from view3
      )
     ) v
group by central, id;

Post a Comment for "Join Solution On Postgres With Many Tables"