Data migration-compact and move --

Source: Internet
Author: User

Data migration-compact and move --

The current requirement is that the following result sets become compact:

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


The idea is to find out each column and then perform a left join:
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.