PostgreSQL,多个表上的FULL OUTER JOIN(所有“组合”)

时间:2018-04-06 10:22:57

标签: sql postgresql

我对PostgrSQL和SQL很新。我正在尝试使用FULL OUTER JOIN函数根据相应的11列连接11个表。您可以在下面找到我的代码:

SELECT * FROM _0_general_view
FULL OUTER JOIN _1_foundation_view
ON _0_general_view.concatenate_0_general = _1_foundation_view.concatenate_1_foundation
FULL OUTER JOIN _1_plinth_view
ON _0_general_view.concatenate_0_general = _1_plinth_view.concatenate_1_plinth
FULL OUTER JOIN _1_rc_beams_columns_view
ON _0_general_view.concatenate_0_general = _1_rc_beams_columns_view.concatenate_1_rc_beams_columns
FULL OUTER JOIN _1_vertical_members_view
ON _0_general_view.concatenate_0_general = _1_vertical_members_view.concatenate_1_vertical_members
FULL OUTER JOIN _2_floor_view
ON _0_general_view.concatenate_0_general = _2_floor_view.concatenate_2_floor
FULL OUTER JOIN _2_horizontal_bands_view
ON _0_general_view.concatenate_0_general = _2_horizontal_bands_view.concatenate_2_horizontal_bands
FULL OUTER JOIN _2_rc_beams_columns_view
ON _0_general_view.concatenate_0_general = _2_rc_beams_columns_view.concatenate_2_rc_beams_columns
FULL OUTER JOIN _2_vertical_members_view
ON _0_general_view.concatenate_0_general = _2_vertical_members_view.concatenate_2_vertical_members
FULL OUTER JOIN _2_walls_view
ON _0_general_view.concatenate_0_general = _2_walls_view.concatenate_2_walls
FULL OUTER JOIN _3_roof_view
ON _0_general_view.concatenate_0_general = _3_roof_view.concatenate_3_roof
;

在上面的代码中,我假设我的表格结构如下:

表1 (_0_general_view)

╔══════════╦═════════╦═════════╗
║   col_1  ║  col_2  ║  col_3  ║
╠══════════╬═════════╬═════════╣
║     a    ║ 3.5046  ║   Jan   ║
║     b    ║ 3.7383  ║   Mar   ║
║     c    ║ 3.9719  ║   Jul   ║
║     d    ║ 6.1915  ║   Feb   ║
╚══════════╩═════════╩═════════╝

表2 (_1_plinth_view)

╔══════════╦═════════╦═════════╗
║   col_4  ║  col_5  ║  col_6  ║
╠══════════╬═════════╬═════════╣
║     a    ║ 2.8846  ║   Dec   ║
║     d    ║ 5.2244  ║   Aug   ║
╚══════════╩═════════╩═════════╝

表n (_3_xxxx_view)

╔══════════╦═════════╦═════════╗
║   col_7  ║  col_8  ║  col_9  ║
╠══════════╬═════════╬═════════╣
║     b    ║ 1.2365  ║   May   ║
║     c    ║ 2.5432  ║   Sep   ║
║     d    ║ 8.1515  ║   Oct   ║
╚══════════╩═════════╩═════════╝

换句话说,我假设col_1(在第一个表/视图中名为concatenate_0_general,名为_0_general_view)包含我需要加入的所有记录,但是如果这不是真的怎么办? ?

我无法找到一种方法来考虑我的11个表中的所有可能组合。是否有可能以某种方式?

如果是这样,考虑到每个表平均有150列,并且每个表中的行数可能大约为10000,您认为这将是一个非常漫长的过程。

编辑:可能我们将不得不以更少的记录进行检索,让我们说300-400左右,因为我们将添加一个WHERE子句来按日期过滤它们。

谢谢,Stefano

2 个答案:

答案 0 :(得分:0)

如果join列的名称相同,则可以使用using

select . . .
from a join
     b
     on (id) join
     c
     on (id) . . .;

在您的情况下,union all和聚合是最好的方法 - 假设您每个“id”最终会有一行:

select id, max(col1) as col1, max(col2) as col2, max(col3) as col3,
       max(col4) as col4, max(col5) as col5, max(col6) as col6
from ((select concatenate_0_general as id, col1, col2, NULL as col3, NULL as col4, NULL as col5, NULL as col6
       from _0_general_view
      ) union all
      (select concatenate_1_foundation, NULL, NULL, col3, col4, NULL, NULL
       from _1_foundation_view
      ) union all
      (select concatenate_1_plinth, NULL, NULL, NULL, NULL, col5, col6
       from _1_plinth_view
      )
     ) x
group by id;

如果这些都不起作用,那么您可以构建完整的ID列表并使用left join

select . . .
from ((select concatenate_0_general as id from _0_general_view
      ) union -- on purpose to remove duplicates
      (select concatenate_1_foundation from _1_foundation_view
      ) union
      (select concatenate_1_plinth from _1_plinth_view
      ) union
      . . .
     ) ids left join
     _0_general_view gv
     on gv.concatenate_0_general = ids.id left join
     . . .

答案 1 :(得分:0)

假设第一列是唯一的(否则您的结果集将会爆炸,无论是左连接还是全连接),您可以先创建所有现有值的列表,然后将所有表连接到左边:

select _0_general_view.*,
   _1_foundation_view.*,
   _1_plinth_view.*
...
from
 (
   select concatenate_0_general as col from _0_general_view
   union 
   select concatenate_1_foundation from _1_foundation_view
   union
   select concatenate_1_plinth from _1_plinth_view
   ...
 ) as dt
left join _0_general_view 
  on col = concatenate_0_general
left join _1_foundation_view
  on col = concatenate_1_foundation
left join _1_plinth_view
  on col = concatenate_1_plinth
...