資料的挪移--變得緊湊,挪移--

來源:互聯網
上載者:User

資料的挪移--變得緊湊,挪移--

  現在的需求是講下列的結果集變得緊湊:

         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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.