如何解决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都是名字,我只想要不同值的聚合列表。
解决方法
一种方法是横向连接:
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
)
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。