简体   繁体   中英

Oracle: Need to copy data from one table to another and format it while copying

I need to format data in a table, so for that i am copying it to another table and while copying i am formatting it. Now sure how to do it. Original Table:

sales_offer_id sales_offer_name, sales_offer_description   PRIORITY ADE_PRIORITIZED deployment_date FROM_ENV
121        SO121        SO121 Desc121        111      Y     01-JAN-17   0
123        SO1          SO1 Desc1            111      Y     01-JAN-01   0
123        SO1          SO1 Desc2            111      Y     01-FEB-17   2
123        SO3          SO1 Desc1            111      Y     01-JAN-17   3
123        SO2          SO1 Desc1            111      Y     21-JAN-17   1
987        SO1          SO1_Desc1            111      Y     22-JAN-17   3

My final Data should be looking like:

sales_offer_id sales_offer_name,                    sales_offer_description             PRIORITY      ADE_PRIORITIZED deployment_date FROM_ENV
121        SO121                            SO121 Desc121                  111          Y       01-JAN-17   0
123        SO1;SO3 eff(01/01/17);SO2 eff(01/21/17)  SO1 Desc1;SO1 Desc2 eff(02/01/17)  111          Y       01-FEB-17   0
987        SO1                          SO1_Desc1                  111          Y       22-JAN-17   3

It can be same table or different table.

I am not sure how to do it.

Try to avoid PL/SQL for such a work. Read about String aggregation techniques from Oracle documentation.

eg if you can use Oracle 11g , you're solution should look like

select sales_offer_id
     , listagg(sales_offer_name||' eff('||deployment_date   ||')' , '|') 
        within group (order by sales_offer_id) as sales_offer_description
     , PRIORITY
     , ADE_PRIORITIZED
     , max(FROM_ENv)
     , max(deployment_date)     
-- your data sample
from (
      select 121                   sales_offer_id
           , 'SO121'               sales_offer_name
           , 'SO121 Desc121'       sales_offer_description
           , 111                   PRIORITY 
           , 'Y'                   ADE_PRIORITIZED
           , '01-JAN-17'           deployment_date
           , 0                     FROM_ENV
                                                                     from dual
union select 123, 'SO1'  , 'SO1 Desc1'    , 111, 'Y', '01-JAN-01', 0 from dual
union select 123, 'SO1'  , 'SO1 Desc2'    , 111, 'Y', '01-FEB-17', 2 from dual
union select 123, 'SO3'  , 'SO1 Desc1'    , 111, 'Y', '01-JAN-17', 3 from dual
union select 123, 'SO2'  , 'SO1 Desc1'    , 111, 'Y', '21-JAN-17', 1 from dual
union select 987, 'SO1'  , 'SO1_Desc1'    , 111, 'Y', '22-JAN-17', 3 from dual
) group by sales_offer_id
     , PRIORITY
     , ADE_PRIORITIZED

... then you may have a little more tweaking to meet your requirement that you haven't thoroughly defined.

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