sql - 用于多列中不同值的 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
. . .
推荐阅读
- azure-template - 如何在 azure 中同时为所有 webapps 创建新警报
- android - 有没有办法在自定义视图中提取 AppBarLayout 和 CollapsingToolbar?
- php - 在ajax中没有得到响应
- jhipster - GKE - 如何在 Jhipster 6 微服务 UAA 项目中的网关上使用 HTTPS
- php - 如果用户查看我的页面,则显示在线,如果他关闭,则显示离线
- angular - 在 Angular 6 上构建 ng build --prod 时出错
- debugging - 如何为 hhvm 致命错误获取更相关的堆栈跟踪(文件名和行号)?
- r - Shiny:在模块内引用 renderUI 的 id 时出错
- macros - Notepad++:记录宏不会记住在列编辑器中执行的步骤
- memory - 为什么 Prometheus 会消耗这么多内存?