Amazon Recomendations

Wednesday, June 20, 2012

Converting Oracle tables' rows to columns by using LISTAGG

hi All,

Not once I been challenged with this particular issue: how to display multiple rows data in a single column?
There are numerous resources for doing so by using XMLAGG which is fair if you go straight to XML output.
In my case, XML wasn't the goal and I rather prefer to have simple fetch in PL/SQL developer.Good thing on 11g we have WITHIN GROUP and LISTAGG to have it the way I need.
Following post provides detailed example of the feature described at http://www.dba-oracle.com/t_converting_rows_columns.htm

Problem: On the Batch Header there are multiple text paragraphs defined to store varied information:














In our case its a text paragraph called Stability Study Requirements used by Quality Personnel. Every text paragraph (the text stored within) associated with his Batch Header by text_code.
(gme_batch_header.text_code = gme_text_table.text_code):
















The problem occurs when same text_code has multiple text lines (long text inserted into the window above) cause single line has only 70 character length, so while trying to fetch the whole Paragraph text we might have several lines retrieved...

Solution: By using within group SQL clause we could pivot multiple Paragraphs' text rows onto a single row and then using the listagg() built-in function to display multiple column values in a single column.


Example: Displaying all Finish Goods' batches that have Stability Study Requirement text paragraph allocated. Whole paragraphs' text displayed as a single column:



select t.batch_no "Batch No",
       t.item_no  "Item Number",
       t.plant_code  "Plant Code",
       text.paragraph_text "Stability Text Paragraph"
from (select    iim.item_no,
                gbh.plant_code,
                gbh.batch_no,
                gbh.text_code
         from   gme_batch_header          gbh,
                gme_material_details      gmd,
                ic_item_mst_b             iim,
                ic_item_mst_b_dfv         im_dfv,
                ic_lots_mst               ilm
         where  1 = 1
           and  gbh.actual_start_date > to_date('01-JAN-2011', 'DD-MON-YYYY')        
           and  gmd.batch_id       = gbh.batch_id
           and  gmd.line_no        = 1
           and  gmd.line_type      = 1
           and  iim.item_id        = gmd.item_id
           and  iim.rowid          = im_dfv.row_id
           and  iim.inactive_ind   = 0
           and  im_dfv.item_type  in 10 --FG
           and  iim.item_id        = ilm.item_id 
           and  ilm.lot_no        <> fnd_profile.value('IC$DEFAULT_LOT')
           and  exists (select 1
                        from gme_text_table_tl  gtt
                        where  gtt.language      = userenv('LANG')
                        and  gtt.lang_code       = userenv('LANG')
                        and  gtt.paragraph_code = 'STAB'
                        and  gtt.text_code = gbh.text_code) 
        group by iim.item_no,
                 gbh.plant_code,
                 gbh.batch_no,
                 gbh.text_code)t
     ,(select gtt.text_code code
              ,listagg (gtt.text, '')
              within group
              (ORDER BY gtt.line_no) paragraph_text
              from gme_text_table_tl         gtt
              where 1 = 1
              and  gtt.language       = userenv('LANG')
              and  gtt.lang_code      = userenv('LANG')
              and  gtt.paragraph_code = 'STAB'
              and  gtt.text is not null
              GROUP BY 
              gtt.text_code)text
where text.code = t.text_code



















Thursday, June 7, 2012

Existing WIP Jobs' components update

hi All,

Spent some time lately trying to update components of already created Work Orders. Seems to be more "interesting" than I though.
First of all, DO NOT RELY ON THE GUIDE for that particular topic!
The guide (Oracle Manufacturing: APIs and Open Interfaces Manual Release 11i Part No. A95955-03 January 2004) misleads (misguides:)) you  regarding this particular issue.
According to Metalink this piece was fixed in the R12 version of the guide, but 11.5.0 release omitted one crucial piece of information - how to start!
The insertion/creation of new Work Orders explained rather decently - first load header and then jobs details:
1. Insert record into WIP_JOB_SCHEDULE_INTERFACE table
2. Insert record into WIP_JOB_DTLS_INTERFACE table
3.submit WIP Mass Load program
I won't elaborate on that one cause there are several detailed examples for it:
http://oracleappss.blogspot.co.il/2008/07/wip-job-creation.html

Work Orders' update process explained pretty thoroughly, so I gave it a try.
According to guide, in order to update jobs' component, you required  to insert the record for the WIP Job you are looking to update into WIP_JOB_DTLS_INTERFACE and submit WIP Mass Load program.

One thing that seemed to be odd and caught my attention is that in case of job components update, the PARENT_HEADER_ID field: ...Must be NULL if only detail records are loaded or updated. Must
provide WIP_ENTITY_ID and ORGANIZATION_ID.... 


Hmm, took me a while to understand and to overcome an implications of that misleading comment.

Solution: For Update, basically, the same scenario should be used as for the Insert:

1. Insert record into WIP_JOB_SCHEDULE_INTERFACE table
2. Insert record into WIP_JOB_DTLS_INTERFACE table
3. Submit WIP Mass Load program

* do consult the guide regarding the required fields.

Following an actual code sample (I been trying to update SUPPLY_SUBINVENTORY filed):

first get "unique" HEADER_ID and GROUP_ID:

select max(t.header_id)+1,
            max(t.group_id)+1
from wip_job_schedule_interface 



and insert the records as follows (those are mostly the required fields allocated)  :


INSERT INTO wip_job_schedule_interface
(
organization_id
, primary_item_id
, job_name
, wip_entity_id
, group_id
, header_id
, load_type
, process_phase
, process_status
, created_by
, creation_date
, last_updated_by
, last_update_date
)
values
(
87 -- organization_id
,329075 -- primary_item_id
,23555--wip_entity_name
,1877088 -- wip_entity_id
,104 -- max(t.group_id)+1
,64798 -- max(t.header_id)+1
,3 -- load_type
,2 -- process_phase
,1 -- process_status
,4672 -- created_by
,SYSDATE -- creation_date
,4672 -- last_updated_by
,SYSDATE -- last_update_date
)



INSERT INTO WIP_JOB_DTLS_INTERFACE (
 organization_id
, operation_seq_num
, wip_entity_id
, inventory_item_id_old
, wip_supply_type
, date_required
, group_id
, parent_header_id
, load_type
, substitution_type
, process_phase
, process_status
, created_by
, creation_date
, last_updated_by
, last_update_date
, SUPPLY_SUBINVENTORY
)
values
(
87 --organization_id
,10 --operation_seq_num
,1877088 --wip_entity_id
,329075 --inventory_item_id_old
,7 --wip_supply_type
,sysdate + 2 --date_required
,104 --max(t.group_id)+1
,64798 --max(t.header_id)+1
,2 --load_type 1. resource 2. component 3. operation 4. multiple resource usage
,3 --substitution_type 1.Delete, 2.Add 3.Change
,2 --process_phase
,1 --process_status
,4672 --created_by
,SYSDATE
,4672 --last_updated_by
, SYSDATE
,'Blah Blah' );

enjoy

Felix