码迷,mamicode.com
首页 > 其他好文 > 详细

数据的挪移--变得紧凑

时间:2015-03-19 10:12:44      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

  现在的需求是讲下列的结果集变得紧凑:

         COL1       COL2       COL3
     ---------- ---------- ----------         

         1
         2
         3
         4
         5
         6
                    7
                    8
                    9
                   10
                              11
                              12


      COL1       COL2       COL3
    ---------- ---------- ----------
         1          7         11
         2          8         12
         3          9
         4         10
         5
         6

drop table test;
create table test(col1 number,col2 number,col3 number);
insert into test values(1,null,null);
insert into test values(2,null,null);
insert into test values(3,null,null);
insert into test values(4,null,null);
insert into test values(5,null,null);
insert into test values(6,null,null);
insert into test values(null,7,null);
insert into test values(null,8,null);
insert into test values(null,9,null);
insert into test values(null,10,null);
insert into test values(null,null,11);
insert into test values(null,null,12);
commit;

SQL> select * from test;
      COL1       COL2       COL3
---------- ---------- ----------
         1
         2
         3
         4
         5
         6
                    7
                    8
                    9
                   10
                              11
                              12


思路是每列查出来,然后做左连接:
with t1 as(select rownum rn,col1 from test where col1 is not null),
t2 as(select rownum rn,col2 from test where col2 is not null),
t3 as(select rownum rn,col3 from test where col3 is not null)
select col1,col2,col3 from t1,t2,t3 where t1.rn=t2.rn(+) and t2.rn=t3.rn(+) 
order by t1.rn;

      COL1       COL2       COL3
---------- ---------- ----------
         1          7         11
         2          8         12
         3          9
         4         10
         5
         6

数据的挪移--变得紧凑

标签:

原文地址:http://blog.csdn.net/stevendbaguo/article/details/44452449

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!