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

时间:2015-08-02 18:21:44

标签: oracle

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

提前感谢您的帮助。

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 )

1 个答案:

答案 0 :(得分:2)

COALESCE可以解决问题,它会选择第一个NOT NULL值

 select DISTINCT async, coalesce(column1,column2,column3,column4,...) from Table1;