【问题标题】:Postgres - Combine two columns with distinct values and get distinct on the combined valuesPostgres - 组合具有不同值的两列并在组合值上获得不同
【发布时间】:2023-01-28 19:48:03
【问题描述】:

我有一个名为 test_table 的表,如下所示

Column Type Collation Nullable Default
ts timestamp with time zone not null
ba integer
ca integer

现在,如果我在 ba 列上运行 distinct,我得到

ba
5
10
11

同样,如果我在 ca 列上运行 distinct,我会得到

ca
5
10
18
20

现在我想合并这两个 distinct 列的值,并从 combined 值中得到一个 distinct,这样输出如下所示

预期产出

combo
5
10
11
18
20

我可以像这样在多列上做一个DISTINCT

select distinct ba, ca from test_table;

但我无法弄清楚如何将这两列组合起来并从中获得不同的值。

【问题讨论】:

    标签: sql postgresql union distinct


    【解决方案1】:

    听起来您是在合并这两列之后:

    select ba
    from test_table
    union
    select ca
    from test_table;
    

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多