示例:
表2(原始表)
custid 01-jan-2018 07-jan-2018 14-jan-2018 21-jan-2018
102 7 2 5 4
需要创建如下表(表1)的表:
custid date data
102 01-jan-2018 7
102 07-jan-2018 2
102 14-jan-2018 5
102 21-jan-2018 4
请告知如何从table2实现/创建table1。
答案 0 :(得分:0)
以下是实现此方案的步骤:
create table Table2
(custid number,
"01-jan-2018" number,
"07-jan-2018" number,
"14-jan-2018" number,
"21-jan-2018" number);
insert into Table2 values (102,7,2,5,4);
create table Table3
(custid number,
"date" date,
"data" number);
insert into Table3(custid,"date","data")
(SELECT *
FROM Table2
UNPIVOT
INCLUDE NULLS
(DATA FOR COL IN
(
"01-jan-2018" ,"07-jan-2018","14-jan-2018","21-jan-2018"
)
));
select * from table3;
102 01-JAN-18 7
102 07-JAN-18 2
102 14-JAN-18 5
102 21-JAN-18 4
答案 1 :(得分:0)
您可以将unpivot
关键字用作:
create table Table1 as
with Table2(custid,d01_Jan_2018,d07_Jan_2018,d14_Jan_2018,d21_Jan_2018) as
(
select 102, 7, 2, 5, 4 from dual
)
select custid, col1 as "DATE", data from Table2
unpivot
(data for col1 in (d01_Jan_2018, d07_Jan_2018, d14_Jan_2018,d21_Jan_2018));
select * from Table1;
CUSTID DATE DATA
------ ------------ -----
102 D01_JAN_2018 7
102 D07_JAN_2018 2
102 D14_JAN_2018 5
102 D21_JAN_2018 4