Link to home
Start Free TrialLog in
Avatar of Jblue R
Jblue R

asked on

Round 2: aggregate & concatenate values from related tables Oracle 12c SQL

Listing this anew in hopes that if I ask a better question, I may get a helpful response from the resident experts.  

I need to use listagg and concatenation to create a field that begins with the track_item_id, adds any and all p_code, add any and all serial_number, and then places the status for each track_item_id,  p_code / serial_number combination.  

There are thousands of records, the example attempts to bring the logic definition to the barest possible groupings.

 To get there, take a cmp_id from the track_item1 table, add a product id from the mipp2 table.  See if those two fields define a record in a third prodlic3 table…  If so, list the p_code and serial_number, then pick up the status_id from there and use it to determine the status coming from pm_product_permit_status table..

Insert the status value into the current concatenation....placing the status after the serial number .. If there is no serial number, add a comma in front of the status

Product status
The prod_permit4 table carries a perrmit_status_id and the tip_status5 table carries the coded status (done--notdone--ignored--etc.)

There will be one only status for each combination. I have tried adding to the concatenation, but find it time I ask for help in putting together the SQL to get it right.

Any and all pointers and suggestion will be greatly appreciated……..
-----------------------------
        •   For each product, take cmp_id from track_item1 table and one of the PRD_ID from mipp2 table.  
             •   See if those two fields define a record in prod3.  If they do, then return the value from tip_status5 corresponding to the permit_status_id in the prod_permit table.
             •   If the cmp_id and PRD_ID do NOT define a record in the PROD3 table, return the value “NotHere”.
-----------------------------

CREATE TABLE MIPP2
(
  TI_ID          VARCHAR2(12 BYTE),
  PR_ID          VARCHAR2(12 BYTE),
  SERIAL_NUMBER  NUMBER(8)
);

CREATE TABLE PRODLIC3
(
  P_ID    VARCHAR2(12 BYTE),
  P_CODE  VARCHAR2(12 BYTE),
  CMP_ID  NUMBER(8)
);

CREATE TABLE PROD_PERMIT4
(
  PRD_ID            NUMBER(8),
  CMP_ID            NUMBER(8),
  PERMIT_STATUS_ID  NUMBER(8)
);

CREATE TABLE TIP_STATUS5
(
  STATUS_ID  NUMBER(8),
  STATUS     VARCHAR2(12 BYTE)
);

CREATE TABLE TRACK_ITEM1
(
  TRACK_ITEM_ID  NUMBER(8),
  CMP_ID         NUMBER(8)
);

Insert into MIPP2
   (TI_ID, PR_ID, SERIAL_NUMBER)
 Values
   ('11', '8', 452);
Insert into MIPP2
   (TI_ID, PR_ID, SERIAL_NUMBER)
 Values
   ('12', '96', NULL);
Insert into MIPP2
   (TI_ID, PR_ID, SERIAL_NUMBER)
 Values
   ('13', '9', NULL);

Insert into PRODLIC3
   (P_ID, P_CODE, CMP_ID)
 Values
   ('8', '66', 5);
Insert into PRODLIC3
   (P_ID, P_CODE, CMP_ID)
 Values
   ('96', '55', 6);
Insert into PRODLIC3
   (P_ID, P_CODE, CMP_ID)
 Values
   ('90', '45', NULL);

Insert into PROD_PERMIT4
   (PRD_ID, CMP_ID, PERMIT_STATUS_ID)
 Values
   (8, 5, 1);
Insert into PROD_PERMIT4
   (PRD_ID, CMP_ID, PERMIT_STATUS_ID)
 Values
   (96, 6, 2);

Insert into TIP_STATUS5
   (STATUS_ID, STATUS)
 Values
   (3, 'ignored');
Insert into TIP_STATUS5
   (STATUS_ID, STATUS)
 Values
   (1, 'done');
Insert into TIP_STATUS5
   (STATUS_ID, STATUS)
 Values
   (2, 'notdone');

Insert into TRACK_ITEM1
   (TRACK_ITEM_ID, CMP_ID)
 Values
   (12, 6);
Insert into TRACK_ITEM1
   (TRACK_ITEM_ID, CMP_ID)
 Values
   (13, 7);
Insert into TRACK_ITEM1
   (TRACK_ITEM_ID, CMP_ID)
 Values
   (11, 5);

Open in new window



Desired Result:

Track_item_id   concatenation
     11                      66 -  452, done
     12                      55 -    , notdone
     13                   NotHere
Avatar of HainKurt
HainKurt
Flag of Canada image

here is a start

select m2.TI_ID,
       p3.p_CODE || ' - ' ||
       m2.SERIAL_NUMBER || ', ' ||
       (case when ts.STATUS is null then 'NotHere' else ts.STATUS end) concatenation
  from MIPP2 m2
  left join PRODLIC3 p3 on m2.pr_id=p3.p_id
  left join PROD_PERMIT4 p4 on p4.prd_id=m2.pr_id
  left join TIP_STATUS5 ts on ts.STATUS_ID=p4.PERMIT_STATUS_ID;

TI_ID	CONCATENATION
11	66 - 452, done
12	55 - , notdone
13	 - , NotHere

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Pls try this -

/*------------------------
select TI_ID Track_item_id  ,
   CASE WHEN  CAST(  ISNULL(p_code,'') as VARCHAR(100)) || '-' ||  CAST(REPLACE(ISNULL(serial_number,''),'0','') as varchar(100)) || ','  || [Status] IS NULL THEN 
	 'Not here' ELSE  CAST(  ISNULL(p_code,'') as VARCHAR(100)) || '-' ||  CAST(REPLACE(ISNULL(serial_number,''),'0','') as varchar(100)) || ','  || [Status] END
	[concatenation] from 
MIPP2 a
LEFT JOIN PRODLIC3 p ON a.PR_ID = p.P_ID
LEFT JOIN PROD_PERMIT4 pr ON pr.prd_id = a.PR_ID
LEFT JOIN TIP_STATUS5 t on t.status_id = pr.permit_status_id
------------------------*/
Track_item_id concatenation
------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
11            66-452,done
12            55-,notdone
13            Not here

(3 row(s) affected)

Open in new window

Avatar of Jblue R
Jblue R

ASKER

It is a start.. thank you ..  I should have added more rows that allow for everything except track_item_id to come up null... can we add nvl2 into your solution to allow for potential nulls in product / serial numbers? product can be null, products can be multiple, and one or more serial_number values  for one or more can be null..

again, thank you for your reply.
Avatar of Jblue R

ASKER

@Pawan    Thank you for your reply.. I removed the brackets around concatenation and status, still retuned error

ORA-00904: "ISNULL": invalid identifier  replacing same with nvl returned desired results.. I am going to add some nullism and check it again.. Thank you !!

noticed that instead of nothere, a your code is returning blanks but with the punctuations.. need to clear that out..

TRACK_ITEM_ID
-------------
CONCATENATION                                                                  
--------------------------------------------------------------------------------
11          
66-452,done                                                                    
                                                                               
12          
55-,notdone                                                                    
                                                                               
13          
-,                                                                              
                                                                               

3 rows selected.

Thank each of you for replying and working with me.. It is appreciated .
Avatar of Jblue R

ASKER

@HainKurt - Bingo !!  We have arrived.!. Your solution covers all potential bases..

 Thank you once again..  It all seems so simply elegant.  My greatest shortcoming seems to be imagination... or a thorough enough knowledge of the language to make the solutions jump out at me as they do youse.

Thank you again, I deeply appreciate your patience.. Have a great weekend !!
Avatar of Jblue R

ASKER

Thank you sir...