oracle条件数据转换成行和列
oracle conditional data transformation into rows and columns
我有一个来源 table 来自如下应用程序,
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
我想使用源 table 数据插入到目标 table B 中,如下所示。
正如您所看到的,联系信息被扁平化为列,而客户信息仍然是行的形式。我怎样才能在 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
像这样使用条件逻辑和聚合:
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]
我有一个来源 table 来自如下应用程序,
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
我想使用源 table 数据插入到目标 table B 中,如下所示。 正如您所看到的,联系信息被扁平化为列,而客户信息仍然是行的形式。我怎样才能在 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
像这样使用条件逻辑和聚合:
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]