I have a source table which comes from an application as below,
ID NAME PHONETYPE PHONENO CUSTOMERID CUSTOMER NAME
1 Chris Work 1234567890 3 Sony
1 Chris Work 1234567890 4 TOM
1 Chris Mobile 0123456789 3 Sony
1 Chris Mobile 0123456789 4 TOM
1 Chris Fax 0000111111 3 Sony
1 Chris Fax 0000111111 4 TOM
2 Ryan Work 1111122222 5 Mary
2 Ryan Work 1111122222 6 Joe
2 Ryan Mobile 2222233333 5 Mary
2 Ryan Mobile 2222233333 6 Joe
I want to use the source table data to insert into a target table B as below. As you can see, the contact information is flattened into columns where as the customer information is still in the form of rows. How can I achieve this in Oracle sql.
ID NAME WORKNO MOBILENO FAXNO CUSTOMERID CUSTOMERNAME
1 Chris 1234567890 0123456789 0000111111 3 Sony
1 Chris 1234567890 0123456789 0000111111 4 Tom
2 Ryan 1111122222 2222233333 NULL 5 Mary
2 Ryan 1111122222 2222233333 NULL 6 Joe
Use conditional logic and aggregate like this:
select id, name, max(case when phonetype='work' then phonno else null end) workno,
max(case when phonetype='mobile' then phonno else null end) mobileno,
max(case when phonetype='fax' then phonno else null end) faxno, customerid,
[customer name]
from yourtable group by id, name,customerid,
[customer name]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.