简体   繁体   中英

Inserting data in another table having different data structure in oracle

I have a table table1

ITEM_CODE             DESC       MONTH      DAY01  DAY02  DAY03
FG0050BCYL0000CD     CYL HEAD    FEB-15       0       204   408
FG00186CYL0000CD     POWER UNIT  FEB-15       425    123    202

I want to insert data in another table table2 from table1 in such a way.

ITEM_CODE                           MONTH        DATE       QUANTITY
FG0050BCYL0000CD                    FEB-15    01-FEB-2015      0
FG0050BCYL0000CD                    FEB-15    02-FEB-2015      204
FG0050BCYL0000CD                    FEB-15    03-FEB-2015      408
FG00186CYL0000CD                    FEB-15    01-FEB-2015      425
FG00186CYL0000CD                    FEB-15    02-FEB-2015      123
FG00186CYL0000CD                    FEB-15    03-FEB-2015      202

Please tell me how to achieve this via pl sql

This SQL query worked for me.

with items as (
  select table1.*, 
      to_date(month||'-01', 'MON-YY-DD', 'NLS_DATE_LANGUAGE=American') day
    from table1)
select item_code, month, day + lvl - 1 day,
    case extract(Day from day + lvl - 1)
      when 1 then day01 
      when 2 then day02
      when 3 then day03    
      -- <- insert rest (day04...day30) here
      when 31 then day31
    end value
  from items
    join (select level lvl from dual connect by level<32) n
      on day + lvl - 1 <= last_day(day)

Subquery items attaches first day of month to data. Next I join this subquery with other, hierarchical subquery, which gives simple list of 31 numbers (form 1 to 31). Join is constructed this way that date cannot exceed last day of month. So for each row in table1 we have 28, 29, 30 or 31 rows with proper dates. Now simple, but tedious task - for each day we have to get value from proper column; we need case here. In solution these are four rows, but you will need to complete rest.

At the end just insert results into table2.

The following should get you close:

BEGIN
  FOR aRow IN (SELECT * FROM TABLE1)
  LOOP
    INSERT INTO TABLE2(ITEM_CODE, MONTH, "DATE", QUANTITY)
      VALUES (aRow.ITEM_CODE, aRow.MONTH,
              TO_DATE(aRow.MONTH, 'MON-RR')+0, aRow.DAY01);

    INSERT INTO TABLE2(ITEM_CODE, MONTH, "DATE", QUANTITY)
      VALUES (aRow.ITEM_CODE, aRow.MONTH,
              TO_DATE(aRow.MONTH, 'MON-RR')+1, aRow.DAY02);

    INSERT INTO TABLE2(ITEM_CODE, MONTH, "DATE", QUANTITY)
      VALUES (aRow.ITEM_CODE, aRow.MONTH,
              TO_DATE(aRow.MONTH, 'MON-RR')+2, aRow.DAY03);
  END LOOP;
END;

Note that the column names DESC and DATE are both reserved words in Oracle, which requires that they be quoted as shown above. It would be simpler to use different names, such as DESCRIPTION and ACTIVITY_DATE , to eliminate the need to quote these names every time they're used.

Best of luck.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM