首页 > 解决方案 > 用于多列中不同值的 Oracle SQL

问题描述

我有 10 列不同任务编号的名称。我想要一份所有名字的清单。

我已经尝试过了,并且收到了 Oracle 错误。

select distinct(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) from x where y is not null;

a1 - a10 都是名称,我只想要不同值的聚合列表。

标签: sqloracledistinct

解决方案


一种方法是横向连接:

select distinct a
from t cross join lateral
     (select t.a1 as a from dual union all
      select t.a2 from dual union all
      select t.a3 from dual union all
      select t.a4 from dual union all
      select t.a5 from dual union all
      select t.a6 from dual union all
      select t.a7 from dual union all
      select t.a8 from dual union all
      select t.a9 from dual union all
      select t.a10 from dual
     ) s
where a is not null;

在早期版本中,您可以使用union all

select a1 from t where a1 is not null
union  -- on purpose to remove duplicates
select a2 from t where a2 is not null
union  -- on purpose to remove duplicates
. . . 

      

推荐阅读