我需要从 oracle 中的另一个 table2 创建一个 oracle table1。我需要在其中插入数据作为 table2 的列名。例子

I need to create a oracle table1 from another table2 in oracle. where i need to insert the data as the column name of table2. example

示例:

表2(原table)

custid   01-jan-2018   07-jan-2018  14-jan-2018  21-jan-2018
102      7               2            5            4

需要创建一个如下所示的 table (Table1):

custid   date             data
102      01-jan-2018       7
102      07-jan-2018       2
102      14-jan-2018       5
102      21-jan-2018       4

请指教如何从 table2.

achieve/create table1

以下是实现此场景的步骤:

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

您可以使用 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

SQL Fiddle Demo