Oracle SQL 用于多列中的不同值
Oracle SQL for Distinct Values in Multiple Columns
我有 10 个列,其中包含不同任务编号的名称。我想要一个清楚地列出所有名字的列表。
我已经试过了,但出现 Oracle 错误。
select distinct(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) from x where y is not null;
a1 - a10 都是名称,我只想要不同值的聚合列表。
一种方法是横向连接:
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
. . .
SELECT distinct A
FROM X
UNPIVOT(
A -- unpivot_clause
FOR A_N -- unpivot_for_clause
IN ( -- unpivot_in_clause
a1,a2,a3,a4,a5,a6,a7,a8,a9,a10
)
);
我有 10 个列,其中包含不同任务编号的名称。我想要一个清楚地列出所有名字的列表。
我已经试过了,但出现 Oracle 错误。
select distinct(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) from x where y is not null;
a1 - a10 都是名称,我只想要不同值的聚合列表。
一种方法是横向连接:
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
. . .
SELECT distinct A
FROM X
UNPIVOT(
A -- unpivot_clause
FOR A_N -- unpivot_for_clause
IN ( -- unpivot_in_clause
a1,a2,a3,a4,a5,a6,a7,a8,a9,a10
)
);