首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Oracle SQL中将10列转换为单个列

在Oracle SQL中将10列转换为单个列
EN

Stack Overflow用户
提问于 2015-08-02 18:21:43
回答 1查看 33关注 0票数 1

是否可以编写此oracle查询的优化版本,使其不执行10次?

提前谢谢你的帮助。

代码语言:javascript
复制
 select async, column1 from
 (
 select distinct async, COLUMN1 from Table1 
 where COLUMN1 is not null 

 union 
 select distinct async, COLUMN2 as COLUMN1 from Table1 and
 where COLUMN1 is null and COLUMN2 is not null 

 union 
 select distinct async, COLUMN3 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and COLUMN3 is not null 

 union 
 select distinct async, COLUMN4 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is not null 

 union 
 select distinct async, COLUMN5 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is null and COLUMN5 is not null 

 union 
 select distinct async, COLUMN6 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is null and COLUMN5 is null and 
 COLUMN6 is not null 

 union 
 select distinct  async, COLUMN7 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is null and COLUMN5 is null and COLUMN6 is null and
 COLUMN7 is not null 

 union 
 select distinct async, COLUMN8 as COLUMN1 from Table1 
 where
 COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is null and COLUMN5 is null and COLUMN6 is null and
 COLUMN7 is null and COLUMN8 is not null 

 union 
 select distinct async, COLUMN9 as COLUMN1 from Table1 
 where
 COLUMN1 is null and COLUMN2 is null and COLUMN3 is null and
 COLUMN4 is null and COLUMN5 is null and COLUMN6 is null and 
 COLUMN7 is null and COLUMN8 is null and COLUMN9 is not null 

 union 
 select distinct async, COLUMN10 as COLUMN1 from Table1 
 where COLUMN1 is null and COLUMN2 is null and
 COLUMN3 is null and COLUMN4 is null and COLUMN5 is null and
 COLUMN6 is null and COLUMN7 is null and COLUMN8 is null and
 COLUMN9 is null and COLUMN10 is not null 
 )

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-02 18:38:48

聚结做到了这一点,它选择第一个非空值

代码语言:javascript
复制
 select DISTINCT async, coalesce(column1,column2,column3,column4,...) from Table1;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31775126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档