Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Shimmy.

Asked: November 10, 2017 - 9:42 pm UTC

Last updated: December 08, 2017 - 6:08 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Sorry if its a weird requirement.

I have an input feed coming in like the following(only two rows per file)..

col1, col2, col3,Attribute_1,Attribute_2,Attribute_3,......,Attribute_n
col1, col2, col3,Value_1,Value_2,Value_3,......,Value_n

Where n can be any number from 1 to 100.

I have to pivot the results into
col1, col2, col3, Attrubute_1, Value_1
col1, col2, col3, Attrubute_2, Value_2
col1, col2, col3, Attrubute_3, Value_3
.....
.....
col1, col2, col3, Attrubute_n, Value_n

Is there a better way other than declaring an external table with 100 Attriubute columns defined and Pivotting the results?

CREATE TABLE TEST_EXTERNAL_TABLE
   (FILE_LINE_NO       NUMBER,
    COL1               VARCHAR2(10),
    COL2               VARCHAR2(10),
    COL3               VARCHAR2(10),
    ATTRIBUTE_1        VARCHAR2(20),
    ATTRIBUTE_2        VARCHAR2(20),
    ATTRIBUTE_3        VARCHAR2(20),
    ......
    .......
    ATTRIBUTE_100      VARCHAR2(20))
   ORGANIZATION EXTERNAL 
    ( TYPE ORACLE_LOADER
      DEFAULT DIRECTORY "EXT_DAT"
      ACCESS PARAMETERS
      (RECORDS DELIMITED BY NEWLINE CHARACTERSET WE8ISO8859P1
      BADFILE DIR_EXT_BAD: 'test_external_table.bad'
      LOGFILE DIR_EXT_LOG: 'test_external_table.log'
       READSIZE 1048576
       FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LRTRIM
       REJECT ROWS WITH ALL NULL FIELDS
  (FILE_LINE_NO       RECNUM,
   COL1               CHAR(10),
   COL2               CHAR(10),
   COL3               CHAR(10),
   ATTRIBUTE_1        CHAR(20),
   ATTRIBUTE_2        CHAR(20),
   ATTRIBUTE_3        CHAR(20),
   .......
   .......
   ATTRIBUTE_100      CHAR(20)
 )
           )
      LOCATION
       ( 'test_external_table.dat'
       )
    );

and Connor said...

One option you could explore is to defer the parsing of the optional attributes until later.

So the external table definition would simply treat ATTRIB_1... ATTRIB_N as a single column.

Then you could a technique like described in this thread:

https://asktom.oracle.com/pls/apex/asktom.search?tag=i-need-to-replace-this-query-with-substr-and-instr

to convert the comma delimited string into rows.

That may will actually be *slower* then your current option, but you'd need to benchmark that to confirm.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.