【问题标题】:Union all in Vertica SQL based on tables with different number of columns?基于具有不同列数的表在 Vertica SQL 中联合所有?
【发布时间】:2021-01-29 11:44:20
【问题描述】:

您好,我在 Vertica SQL 中有两个表:

表 1

col1  col2  col3
1      3    5
2      4    6

表 2

col1  col2
11    33
22    44

我想联合这两个表,因此我想拥有:

col1  col2  col3
1      3     5
2      4     6
11     33    NULL
22     44    NULL

如何在vertica中做到这一点

【问题讨论】:

    标签: sql union vertica


    【解决方案1】:

    一般来说,您应该使用UNION ALL 并使用您想要的任何默认值定义额外的列:

    select col1, col2, col3
    from table1
    union all
    select col1, col2, NULL as col3
    from table2;
    

    UNION 会产生删除重复项的开销。一般来说,您应该使用UNION ALL,除非您打算删除重复项。

    【讨论】:

      【解决方案2】:

      如下使用null

      select col1, col2, col3 from table1
      union 
      select col1, col2, null from table2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 2013-11-19
        • 2011-09-09
        • 1970-01-01
        • 2021-12-26
        • 1970-01-01
        相关资源
        最近更新 更多