CREATE TABLE SRC_CHECKED_OUT_PRIMARY_KEY AS (
		 		 		 		 		 SELECT DISTINCT itm_primary_key
                                        FROM itm,
                                             coa
                                        WHERE ITM_COLLAB_INFO is not null and ITM_COLLAB_INFO <> ' '
             		 		 		 		       AND LOCATE(ITM_COLLAB_INFO, '::' || RTRIM(CHAR(COA_ID)) || '||') > 0
                                              AND itm_container_id = coa_src_container_id
                                              AND itm_next_version_id = 999999999
                                              AND COA_ID = 4
                                        UNION 
                                        SELECT DISTINCT itm_primary_key
                                        FROM itm,
                                             coa
                                        WHERE ITM_COLLAB_INFO is not null and ITM_COLLAB_INFO <> ' '
		 		 		 		 AND SUBSTR (ITM_COLLAB_INFO,(length(ITM_COLLAB_INFO)-(length(rtrim(char(COA_ID)))+1))) = '::'||rtrim(char(COA_ID))
                                              AND itm_container_id = coa_src_container_id
                                              AND itm_next_version_id = 999999999
                                              AND COA_ID = 4) definition only in users;




CREATE TABLE CA_ALL_PRIMARY_KEY AS (
		 		 		 		 SELECT DISTINCT itm_primary_key
                               FROM itm,
                                    coa
                               WHERE itm_container_id = COA_DST_CONTAINER_ID
                                     AND itm_next_version_id = 999999999
                                     AND COA_ID = 4) definition only in users;



CREATE TABLE SRC_ALL_PRIMARY_KEY AS (
		 		 		 		 SELECT DISTINCT itm_primary_key
                                FROM itm,
                                     coa
                                WHERE itm_next_version_id = 999999999
                                      AND itm_container_id = COA_SRC_CONTAINER_ID
                                      AND COA_ID = 4) definition only in users;


CREATE TABLE ERROR_EVENT_ITEM_ID AS (
		 		 		 		 SELECT DISTINCT (wee_entry_id)
                                FROM wee,
                                     wfe
                                WHERE wfe_id = wee_event_id
                                      AND wfe_collaboration_area_id = 4
                                      AND wfe_event_status = 'ERROR') definition only in users;





CREATE TABLE NEW_EVENT_ITEM_ID AS (
		 		 		       SELECT DISTINCT (wee_entry_id)
                              FROM wee,
                                   wfe
                              WHERE wfe_id = wee_event_id
                                    AND wfe_collaboration_area_id = 4
                                    AND wfe_event_status = 'NEW') definition only in users;



CREATE TABLE DONE_EVENT_ITEM_ID AS (
		 		 		 		 SELECT DISTINCT (wee_entry_id)
                               FROM wee,
                                    wfe
                               WHERE wfe_id = wee_event_id
                                     AND wfe_collaboration_area_id = 4
                                     AND wfe_event_status = 'DONE') definition only in users;




CREATE TABLE PROCESSING_EVENT_ITEM_ID AS (
		 		 		 		      SELECT DISTINCT (wee_entry_id)
                                     FROM wee,
                                          wfe
                                     WHERE wfe_id = wee_event_id
                                           AND wfe_collaboration_area_id = 4
                                           AND wfe_event_status = 'PROCESSING') definition only in users;



CREATE TABLE GOOD_EVENT_ITEM_ID AS (
		 		 		        SELECT DISTINCT wee_entry_id
                               FROM wee,
                                    wfe
                               WHERE wfe_id = wee_event_id
                                     AND wfe_collaboration_area_id = 4
                               EXCEPT 
                               (SELECT * 
                               FROM ERROR_EVENT_ITEM_ID
                               UNION 
                               SELECT * 
                               FROM PROCESSING_EVENT_ITEM_ID)) definition only in users;




CREATE TABLE SRC_PRIMARY_KEY_IN_CEM AS (
		 		   SELECT DISTINCT itm_primary_key
                                   FROM itm i,
                                        coa
                                   WHERE i.itm_next_version_id = 999999999
                                         AND i.itm_container_id = COA_SRC_CONTAINER_ID
                                         AND COA_ID = 4
                                         AND EXISTS(SELECT 1
                                                    FROM itm j,
                                                         cem,
                                                         GOOD_EVENT_ITEM_ID
                                                    WHERE i.itm_primary_key = j.itm_primary_key
                                                          AND j.itm_next_version_id = 999999999
                                                          AND j.itm_container_id = COA_DST_CONTAINER_ID
                                                          AND cem_src_entry_id = i.itm_id
                                                          AND cem_ca_entry_id = j.itm_id
                                                          AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id)) definition only in users;
                                                          
                                                          



CREATE TABLE SRC_PRIMARY_KEY_IN_CEM_CAE AS ( 
					SELECT DISTINCT i.itm_primary_key
                                       FROM itm i,
                                            coa
                                       WHERE itm_next_version_id = 999999999
                                             AND itm_container_id = COA_SRC_CONTAINER_ID
                                             AND COA_ID = 4
                                             AND EXISTS(SELECT 1
                                                        FROM itm j,
                                                             cem,
                                                             cae,
                                                             GOOD_EVENT_ITEM_ID
                                                        WHERE i.itm_primary_key = j.itm_primary_key
                                                              AND j.itm_next_version_id = 999999999
                                                              AND j.itm_container_id = COA_DST_CONTAINER_ID
                                                              AND cem_src_entry_id = i.itm_id
                                                              AND cem_ca_entry_id = j.itm_id
                                                              AND cem_ca_entry_id = cae_entry_id
                                                              AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id)) definition only in users;
                                                              



CREATE TABLE SRC_PRIMARY_KEY_NOT_IN_CEM_CAE AS (
		 		 		 		 		 		 SELECT * 
                                           FROM SRC_ALL_PRIMARY_KEY
                                           EXCEPT 
                                           SELECT * 
                                           FROM SRC_PRIMARY_KEY_IN_CEM_CAE) definition only in users;
                                           




CREATE TABLE SRC_PRIMARY_KEY_EVENT_ONLY AS ( 
		 		 		 		 		 SELECT DISTINCT s1.itm_primary_key
                                       FROM coa,
                                            itm s1,
                                            SRC_PRIMARY_KEY_NOT_IN_CEM_CAE,
                                            GOOD_EVENT_ITEM_ID,
                                            NEW_EVENT_ITEM_ID
                                       WHERE COA_ID = 4
                                             AND s1.itm_primary_key = SRC_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
                                             AND s1.itm_next_version_id = 999999999
                                             AND s1.itm_id = GOOD_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_id = NEW_EVENT_ITEM_ID.wee_entry_id
                                             AND (s1.itm_container_id = COA_SRC_CONTAINER_ID
                                                  OR (s1.itm_container_id = COA_DST_CONTAINER_ID
                                                      AND (NOT EXISTS(SELECT 1
                                                                      FROM cae
                                                                      WHERE cae_entry_id = s1.itm_id))
                                                      AND (EXISTS(SELECT 1
                                                                  FROM cem
                                                                  WHERE cem_ca_entry_id = s1.itm_id))))) definition only in users;
                                                                  




--CREATE TABLE SRC_PRIMARY_KEY_OK_CEL AS (
--		 		 		 		 		 SELECT DISTINCT s1.itm_primary_key
--                                   FROM SRC_CHECKED_OUT_PRIMARY_KEY s1,
--                                        itm s2
--                                   WHERE s2.itm_primary_key = s1.itm_primary_key
--                                         AND s2.itm_next_version_id = 999999999
--                                         AND ((NOT EXISTS(SELECT 1
--                                                          FROM cel
--                                                          WHERE itm_id = cel_entry_id))
--                                              OR (EXISTS(SELECT 1
--                                                         FROM cel,
--                                                              cae
--                                                         WHERE itm_id = cel_entry_id
--                                                               AND itm_id = cae_entry_id
--                                                               AND cae_lock_id = cel_id)))) definition only in users;


CREATE TABLE SRC_PRIMARY_KEY_OK_CEL AS (
SELECT DISTINCT s1.itm_primary_key
                                   FROM SRC_CHECKED_OUT_PRIMARY_KEY s1,
                                        itm s2
                                   WHERE s2.itm_primary_key = s1.itm_primary_key
                                         AND s2.itm_next_version_id = 999999999
                                         AND ((NOT EXISTS(SELECT 1
                                                          FROM cel
                                                          WHERE s2.itm_id = cel_entry_id))
                                              OR (EXISTS(SELECT 1
                                                         FROM cel,
                                                              cae
                                                         WHERE s2.itm_id = cel_entry_id
                                                               AND s2.itm_id = cae_entry_id
                                                               AND cae_lock_id = cel_id)))) definition only in users;


CREATE TABLE SRC_GOOD_PRIMARY_KEY AS (
		 		 (SELECT * 
                                 FROM SRC_PRIMARY_KEY_IN_CEM_CAE
                                 UNION 
                                 SELECT * 
                                 FROM SRC_PRIMARY_KEY_EVENT_ONLY)
                                 INTERSECT 
                                 SELECT * 
                                 FROM SRC_PRIMARY_KEY_OK_CEL) definition only in users;
                                 



CREATE TABLE SRC_BAD_PRIMARY_KEY AS (
		 		 		 		 SELECT * 
                                FROM SRC_CHECKED_OUT_PRIMARY_KEY
                                EXCEPT 
                                SELECT * 
                                FROM SRC_GOOD_PRIMARY_KEY) definition only in users;
                                


CREATE TABLE ADD_PRIMARY_KEY AS (
		 		 		 		 SELECT * 
                            FROM CA_ALL_PRIMARY_KEY
                            EXCEPT 
                            SELECT * 
                            FROM SRC_CHECKED_OUT_PRIMARY_KEY) definition only in users;
                            



CREATE TABLE ADD_PRIMARY_KEY_IN_CEM_CAE AS (
		 		 		 		 		 SELECT DISTINCT s1.itm_primary_key
                                       FROM ADD_PRIMARY_KEY s1,
                                            itm s2,
                                            coa
                                       WHERE s2.itm_primary_key = s1.itm_primary_key
                                             AND s2.itm_next_version_id = 999999999
                                             AND s2.itm_container_id = COA_DST_CONTAINER_ID
                                             AND COA_ID = 4
                                             AND EXISTS(SELECT 1
                                                        FROM cem,
                                                             cae,
                                                             GOOD_EVENT_ITEM_ID
                                                        WHERE cem_ca_entry_id = S2.itm_id
                                                              AND cem_ca_entry_id = cae_entry_id
                                                              AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id)) definition only in users;
                                                              



CREATE TABLE ADD_PRIMARY_KEY_NOT_IN_CEM_CAE AS (
		 		 		 		 		 		 SELECT * 
                                           FROM ADD_PRIMARY_KEY
                                           EXCEPT 
                                           SELECT * 
                                           FROM ADD_PRIMARY_KEY_IN_CEM_CAE) definition only in users;
                                           



CREATE TABLE ADD_PRIMARY_KEY_EVENT_ONLY AS (
		 		 		 		 		 SELECT DISTINCT s1.itm_primary_key
                                       FROM coa,
                                            itm s1,
                                            ADD_PRIMARY_KEY_NOT_IN_CEM_CAE,
                                            GOOD_EVENT_ITEM_ID,
                                            NEW_EVENT_ITEM_ID
                                       WHERE COA_ID = 4
                                             AND s1.itm_primary_key = ADD_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
                                             AND s1.itm_next_version_id = 999999999
                                             AND s1.itm_id = GOOD_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_id = NEW_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_container_id = COA_DST_CONTAINER_ID
                                             AND (NOT EXISTS(SELECT 1
                                                             FROM cae
                                                             WHERE cae_entry_id = s1.itm_id))
                                             AND (EXISTS(SELECT 1
                                                         FROM cem
                                                         WHERE cem_ca_entry_id = s1.itm_id))) definition only in users;
                                                         




CREATE TABLE ADD_PRIMARY_KEY_OK_CEL AS (
		 		 		 		 		 SELECT DISTINCT s1.itm_primary_key
                                   FROM ADD_PRIMARY_KEY s1,
                                        itm s2
                                   WHERE s2.itm_primary_key = s1.itm_primary_key
                                         AND ((NOT EXISTS(SELECT 1
                                                          FROM cel
                                                          WHERE s2.itm_id = cel_entry_id))
                                              OR (EXISTS(SELECT 1
                                                         FROM cel,
                                                              cae
                                                         WHERE s2.itm_id = cel_entry_id
                                                               AND s2.itm_id = cae_entry_id
                                                               AND cae_lock_id = cel_id)))) definition only in users;
                                                               


CREATE TABLE ADD_GOOD_PRIMARY_KEY AS (
		 		 (SELECT * 
                                 FROM ADD_PRIMARY_KEY_IN_CEM_CAE
                                 UNION 
                                 SELECT * 
                                 FROM ADD_PRIMARY_KEY_EVENT_ONLY)
                                 INTERSECT 
                                 SELECT * 
                                 FROM ADD_PRIMARY_KEY_OK_CEL) definition only in users;
                                 



CREATE TABLE ADD_BAD_PRIMARY_KEY AS (
		 		 		 		 SELECT * 
                                FROM ADD_PRIMARY_KEY
                                EXCEPT 
                                SELECT * 
                                FROM ADD_GOOD_PRIMARY_KEY) definition only in users;
                                



CREATE TABLE IN_INITIAL_PRIMARY_KEY AS (
		 		 		 		 		 SELECT DISTINCT itm_primary_key
                                   FROM itm,
                                        cae,
                                        coa,
                                        wfd,
                                        spt
                                   WHERE CAE_ENTRY_ID = itm_id
                                         AND itm_next_version_id = 999999999
                                         AND coa_wfl_id = wfd_id
                                         AND coa_wfl_id = spt_leaf_wfl_id
                                         AND cae_step_path_id = spt_id
                                         AND spt_leaf_step_text_path LIKE 'Initial'
                                         AND CAE_CA_ID = 4) definition only in users;
                                         



CREATE TABLE CA_ITM_NO_ITA_PRIMARY_KEY AS (
		 		 		 		 		 SELECT DISTINCT itm_primary_key
                                      FROM itm,
                                           coa
                                      WHERE itm_container_id = COA_DST_CONTAINER_ID
                                            AND itm_next_version_id = 999999999
                                            AND COA_ID = 4
                                            AND NOT EXISTS(SELECT 1
                                                           FROM ita
                                                           WHERE ITA_ITEM_ID = itm_id
                                                                 AND ita_next_version_id = 999999999)) definition only in users;
                                                                 




CREATE TABLE CAE_ORPHAN_ITEM_ID AS (
		 		 		 		 SELECT DISTINCT cae_entry_id
                               FROM cae
                               WHERE cae_ca_id = 4
                                     AND NOT EXISTS(SELECT 1
                                                    FROM itm,
                                                         coa
                                                    WHERE itm_next_version_id = 999999999
                                                          AND COA_ID = 4
                                                          AND itm_container_id = COA_DST_CONTAINER_ID
                                                          AND itm_id = cae_entry_id)) definition only in users;
                                                          



CREATE TABLE CEM_SRC_ORPHAN_ITEM_ID AS (
		 		 		 		 SELECT DISTINCT cem_src_entry_id
                                   FROM cem
                                   WHERE cem_ca_id = 4
                                         AND cem_src_entry_id > 0
                                         AND NOT EXISTS(SELECT 1
                                                        FROM itm,
                                                             coa
                                                        WHERE itm_next_version_id = 999999999
                                                              AND COA_ID = 4
                                                              AND itm_container_id = COA_SRC_CONTAINER_ID
                                                              AND itm_id = cem_src_entry_id)) definition only in users;
                                                              



CREATE TABLE CEM_CA_ORPHAN_ITEM_ID AS (
		 		 		 		 		 SELECT DISTINCT cem_ca_entry_id
                                  FROM cem
                                  WHERE cem_ca_id = 4
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = cem_ca_entry_id)) definition only in users;
                                                             



CREATE TABLE CEL_CA_ORPHAN_ITEM_ID AS (
		 		 		 		 		 SELECT DISTINCT cel_entry_id
                                  FROM cel
                                  WHERE cel_container_id = 4
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = cel_entry_id)) definition only in users;
                                                             


CREATE TABLE WEE_CA_ORPHAN_ITEM_ID AS (
		 		 		 		 		 SELECT DISTINCT wee_entry_id
                                  FROM wee,
                                       wfe
                                  WHERE wfe_collaboration_area_id = 4
                                        AND wfe_id = wee_event_id
                                        AND (wfe_event_status = 'NEW'
                                             OR wfe_event_status = 'PROCESSING'
                                             OR wfe_event_status = 'ERROR')
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = wee_entry_id)) definition only in users;
                                                             



CREATE TABLE CA_ITA_NO_ITM AS (
		 		 		 		 SELECT DISTINCT ita_item_id
                          FROM ita,
                               coa
                          WHERE COA_ID = 4
                                AND ita_catalog_id = COA_DST_CONTAINER_ID
                                AND ita_next_version_id = 999999999
                                AND NOT EXISTS(SELECT 1
                                               FROM itm
                                               WHERE ITA_ITEM_ID = itm_id
                                                     AND itm_next_version_id = 999999999)) definition only in users;
                                                     



CREATE TABLE CA_ITM_NO_ITA AS (
		 		 		 		 SELECT DISTINCT itm_id
                          FROM itm,
                               coa
                          WHERE COA_ID = 4
                                AND itm_container_id = COA_DST_CONTAINER_ID
                                AND itm_next_version_id = 999999999
                                AND NOT EXISTS(SELECT 1
                                               FROM ita
                                               WHERE ITA_ITEM_ID = itm_id
                                                     AND ita_next_version_id = 999999999)) definition only in users;
                                                     



CREATE TABLE TOTAL_BAD_PRIMARY_KEY AS (
		 		 		 		 		 SELECT * 
                                  FROM IN_INITIAL_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM SRC_BAD_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM ADD_BAD_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM CA_ITM_NO_ITA_PRIMARY_KEY) definition only in users;
                                  



CREATE TABLE TOTAL_BAD_ITEM_EVTQ AS (
		 		 		 		 SELECT * 
                                FROM ERROR_EVENT_ITEM_ID
                                UNION 
                                SELECT * 
                                FROM PROCESSING_EVENT_ITEM_ID
                                UNION 
                                SELECT * 
                                FROM WEE_CA_ORPHAN_ITEM_ID) definition only in users;
                                



CREATE TABLE ADDITIONAL_BAD_CA_ITEM_ID (ABCII_ITM_ID) AS (
		 		 		 		 		 SELECT * 
                                      FROM CAE_ORPHAN_ITEM_ID
                                      UNION 
                                      SELECT * 
                                      FROM CEM_CA_ORPHAN_ITEM_ID
                                      UNION 
                                      SELECT * 
                                      FROM TOTAL_BAD_ITEM_EVTQ
                                      UNION 
                                      SELECT * 
                                      FROM CA_ITA_NO_ITM
                                      UNION 
                                      SELECT * 
                                      FROM CA_ITM_NO_ITA) definition only in users;
                                      



CREATE TABLE ADDITIONAL_BAD_SRC_ITEM_ID AS (
		 		 		 		 		 SELECT * 
                                       FROM CEM_SRC_ORPHAN_ITEM_ID) definition only in users;
                                       



CREATE TABLE TOTAL_BAD_CA_ITEM_ID (TBCII_ITM_ID) AS (
		 		 		 		 SELECT DISTINCT x.itm_id
                                 FROM itm x,
                                      TOTAL_BAD_PRIMARY_KEY y,
                                      coa
                                 WHERE x.itm_primary_key = y.itm_primary_key
                                       AND coa_id = 4
                                       AND x.itm_container_id = coa_dst_container_id
                                       AND x.itm_next_version_id = 999999999
                                 UNION 
                                 SELECT * 
                                 FROM ADDITIONAL_BAD_CA_ITEM_ID) definition only in users;
                                 


CREATE TABLE TOTAL_BAD_SRC_ITEM_ID (TBSII_ITM_ID) AS (
		 		 		 		 		 SELECT DISTINCT x.itm_id
                                  FROM itm x,
                                       TOTAL_BAD_PRIMARY_KEY y,
                                       coa
                                  WHERE x.itm_primary_key = y.itm_primary_key
                                        AND coa_id = 4
                                        AND x.itm_container_id = coa_src_container_id
                                        AND x.itm_next_version_id = 999999999
                                  UNION 
                                  SELECT * 
                                  FROM CEM_SRC_ORPHAN_ITEM_ID) definition only in users;
                                  



CREATE TABLE TOTAL_GOOD_PRIMARY_KEY AS (
		 		   (SELECT * 
                                   FROM ADD_GOOD_PRIMARY_KEY
                                   UNION 
                                   SELECT * 
                                   FROM SRC_GOOD_PRIMARY_KEY)
                                   EXCEPT 
                                   SELECT * 
                                   FROM TOTAL_BAD_PRIMARY_KEY) definition only in users;
                                   



CREATE TABLE TOTAL_GOOD_CA_ITEM_ID AS (
		 		 		 		 		 SELECT DISTINCT x.itm_id
                                  FROM itm x,
                                       TOTAL_GOOD_PRIMARY_KEY y,
                                       coa
                                  WHERE x.itm_primary_key = y.itm_primary_key
                                        AND coa_id = 4
                                        AND x.itm_container_id = coa_dst_container_id
                                        AND x.itm_next_version_id = 999999999) definition only in users;
                                        



CREATE TABLE TOTAL_GOOD_SRC_ITEM_ID AS (
		 		 		 		 		 SELECT DISTINCT x.itm_id
                                   FROM itm x,
                                        TOTAL_GOOD_PRIMARY_KEY y,
                                        coa
                                   WHERE x.itm_primary_key = y.itm_primary_key
                                         AND coa_id = 4
                                         AND x.itm_container_id = coa_src_container_id
                                         AND x.itm_next_version_id = 999999999) definition only in users;
                                         


COMMIT;



INSERT INTO SRC_CHECKED_OUT_PRIMARY_KEY SELECT DISTINCT itm_primary_key
                                        FROM itm,
                                             coa
                                        WHERE ITM_COLLAB_INFO is not null and ITM_COLLAB_INFO <> ' '
             		 		  		 		  		 		  		 		        AND LOCATE(ITM_COLLAB_INFO, '::' || RTRIM(CHAR(COA_ID)) || '||') > 0
                                              AND itm_container_id = coa_src_container_id
                                              AND itm_next_version_id = 999999999
                                              AND COA_ID = 4
                                        UNION 
                                        SELECT DISTINCT itm_primary_key
                                        FROM itm,
                                             coa
                                        WHERE ITM_COLLAB_INFO is not null and ITM_COLLAB_INFO <> ' '
		 		  		 		  		 		  		 		  AND SUBSTR (ITM_COLLAB_INFO,(length(ITM_COLLAB_INFO)-(length(rtrim(char(COA_ID)))+1))) = '::'||rtrim(char(COA_ID))
                                              AND itm_container_id = coa_src_container_id
                                              AND itm_next_version_id = 999999999
                                              AND COA_ID = 4;




INSERT INTO CA_ALL_PRIMARY_KEY 
		 		  		 		  		 		  		 		  SELECT DISTINCT itm_primary_key
                               FROM itm,
                                    coa
                               WHERE itm_container_id = COA_DST_CONTAINER_ID
                                     AND itm_next_version_id = 999999999
                                     AND COA_ID = 4;



INSERT INTO SRC_ALL_PRIMARY_KEY 
		 		  		 		  		 		  		 		  SELECT DISTINCT itm_primary_key
                                FROM itm,
                                     coa
                                WHERE itm_next_version_id = 999999999
                                      AND itm_container_id = COA_SRC_CONTAINER_ID
                                      AND COA_ID = 4;


INSERT INTO ERROR_EVENT_ITEM_ID 
		 		  		 		  		 		  		 		  SELECT DISTINCT (wee_entry_id)
                                FROM wee,
                                     wfe
                                WHERE wfe_id = wee_event_id
                                      AND wfe_collaboration_area_id = 4
                                      AND wfe_event_status = 'ERROR';





INSERT INTO NEW_EVENT_ITEM_ID 
		 		  		 		  		 		        SELECT DISTINCT (wee_entry_id)
                              FROM wee,
                                   wfe
                              WHERE wfe_id = wee_event_id
                                    AND wfe_collaboration_area_id = 4
                                    AND wfe_event_status = 'NEW';



INSERT INTO DONE_EVENT_ITEM_ID 
		 		  		 		  		 		  		 		  SELECT DISTINCT (wee_entry_id)
                               FROM wee,
                                    wfe
                               WHERE wfe_id = wee_event_id
                                     AND wfe_collaboration_area_id = 4
                                     AND wfe_event_status = 'DONE';




INSERT INTO PROCESSING_EVENT_ITEM_ID 
		 		  		 		  		 		  		 		       SELECT DISTINCT (wee_entry_id)
                                     FROM wee,
                                          wfe
                                     WHERE wfe_id = wee_event_id
                                           AND wfe_collaboration_area_id = 4
                                           AND wfe_event_status = 'PROCESSING';



INSERT INTO GOOD_EVENT_ITEM_ID 
		 		  		 		  		 		         SELECT DISTINCT wee_entry_id
                               FROM wee,
                                    wfe
                               WHERE wfe_id = wee_event_id
                                     AND wfe_collaboration_area_id = 4
                               EXCEPT 
                               (SELECT * 
                               FROM ERROR_EVENT_ITEM_ID
                               UNION 
                               SELECT * 
                               FROM PROCESSING_EVENT_ITEM_ID);




INSERT INTO SRC_PRIMARY_KEY_IN_CEM SELECT DISTINCT itm_primary_key
                                   FROM itm i,
                                        coa
                                   WHERE i.itm_next_version_id = 999999999
                                         AND i.itm_container_id = COA_SRC_CONTAINER_ID
                                         AND COA_ID = 4
                                         AND EXISTS(SELECT 1
                                                    FROM itm j,
                                                         cem,
                                                         GOOD_EVENT_ITEM_ID
                                                    WHERE i.itm_primary_key = j.itm_primary_key
                                                          AND j.itm_next_version_id = 999999999
                                                          AND j.itm_container_id = COA_DST_CONTAINER_ID
                                                          AND cem_src_entry_id = i.itm_id
                                                          AND cem_ca_entry_id = j.itm_id
                                                          AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id);
                                                          



--INSERT INTO SRC_PRIMARY_KEY_IN_CEM_CAE SELECT DISTINCT itm_primary_key
--                                       FROM itm,
--                                            coa
--                                       WHERE itm_next_version_id = 999999999
--                                             AND itm_container_id = COA_SRC_CONTAINER_ID
--                                             AND COA_ID = 4
--                                             AND EXISTS(SELECT 1
--                                                        FROM itm,
--                                                             cem,
--                                                             cae,
--                                                             GOOD_EVENT_ITEM_ID
--                                                        WHERE itm_primary_key = itm_primary_key
--                                                              AND itm_next_version_id = 999999999
--                                                              AND itm_container_id = COA_DST_CONTAINER_ID
--                                                              AND cem_src_entry_id = itm_id
--                                                              AND cem_ca_entry_id = itm_id
--                                                              AND cem_ca_entry_id = cae_entry_id
--                                                              AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id);

INSERT INTO SRC_PRIMARY_KEY_IN_CEM_CAE
SELECT DISTINCT i.itm_primary_key
                                       FROM itm i,
                                            coa
                                       WHERE itm_next_version_id = 999999999
                                             AND itm_container_id = COA_SRC_CONTAINER_ID
                                             AND COA_ID = 4
                                             AND EXISTS(SELECT 1
                                                        FROM itm j,
                                                             cem,
                                                             cae,
                                                             GOOD_EVENT_ITEM_ID
                                                        WHERE i.itm_primary_key = j.itm_primary_key
                                                              AND j.itm_next_version_id = 999999999
                                                              AND j.itm_container_id = COA_DST_CONTAINER_ID
                                                              AND cem_src_entry_id = i.itm_id
                                                              AND cem_ca_entry_id = j.itm_id
                                                              AND cem_ca_entry_id = cae_entry_id
                                                              AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id);
                                                              



INSERT INTO SRC_PRIMARY_KEY_NOT_IN_CEM_CAE 
		 		  		 		  		 		  		 		  		 		  		 		  SELECT * 
                                           FROM SRC_ALL_PRIMARY_KEY
                                           EXCEPT 
                                           SELECT * 
                                           FROM SRC_PRIMARY_KEY_IN_CEM_CAE;
                                           




INSERT INTO SRC_PRIMARY_KEY_EVENT_ONLY SELECT DISTINCT s1.itm_primary_key
                                       FROM coa,
                                            itm s1,
                                            SRC_PRIMARY_KEY_NOT_IN_CEM_CAE,
                                            GOOD_EVENT_ITEM_ID,
                                            NEW_EVENT_ITEM_ID
                                       WHERE COA_ID = 4
                                             AND s1.itm_primary_key = SRC_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
                                             AND s1.itm_next_version_id = 999999999
                                             AND s1.itm_id = GOOD_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_id = NEW_EVENT_ITEM_ID.wee_entry_id
                                             AND (s1.itm_container_id = COA_SRC_CONTAINER_ID
                                                  OR (s1.itm_container_id = COA_DST_CONTAINER_ID
                                                      AND (NOT EXISTS(SELECT 1
                                                                      FROM cae
                                                                      WHERE cae_entry_id = s1.itm_id))
                                                      AND (EXISTS(SELECT 1
                                                                  FROM cem
                                                                  WHERE cem_ca_entry_id = s1.itm_id))));
                                                                  




--INSERT INTO SRC_PRIMARY_KEY_OK_CEL 
--		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT s1.itm_primary_key
--                                   FROM SRC_CHECKED_OUT_PRIMARY_KEY s1,
--                                        itm s2
--                                   WHERE s2.itm_primary_key = s1.itm_primary_key
--                                         AND s2.itm_next_version_id = 999999999
--                                         AND ((NOT EXISTS(SELECT 1
--                                                          FROM cel
--                                                          WHERE itm_id = cel_entry_id))
--                                              OR (EXISTS(SELECT 1
--                                                         FROM cel,
--                                                              cae
--                                                         WHERE itm_id = cel_entry_id
--                                                               AND itm_id = cae_entry_id
--                                                               AND cae_lock_id = cel_id)));


INSERT INTO SRC_PRIMARY_KEY_OK_CEL
SELECT DISTINCT s1.itm_primary_key
                                   FROM SRC_CHECKED_OUT_PRIMARY_KEY s1,
                                        itm s2
                                   WHERE s2.itm_primary_key = s1.itm_primary_key
                                         AND s2.itm_next_version_id = 999999999
                                         AND ((NOT EXISTS(SELECT 1
                                                          FROM cel
                                                          WHERE s2.itm_id = cel_entry_id))
                                              OR (EXISTS(SELECT 1
                                                         FROM cel,
                                                              cae
                                                         WHERE s2.itm_id = cel_entry_id
                                                               AND s2.itm_id = cae_entry_id
                                                               AND cae_lock_id = cel_id)));
                                                               



INSERT INTO SRC_GOOD_PRIMARY_KEY 
		 		 (SELECT * 
                                 FROM SRC_PRIMARY_KEY_IN_CEM_CAE
                                 UNION 
                                 SELECT * 
                                 FROM SRC_PRIMARY_KEY_EVENT_ONLY)
                                 INTERSECT 
                                 SELECT * 
                                 FROM SRC_PRIMARY_KEY_OK_CEL;
                                 



INSERT INTO SRC_BAD_PRIMARY_KEY 
		 		  		 		  		 		  		 		  SELECT * 
                                FROM SRC_CHECKED_OUT_PRIMARY_KEY
                                EXCEPT 
                                SELECT * 
                                FROM SRC_GOOD_PRIMARY_KEY;
                                


INSERT INTO ADD_PRIMARY_KEY 
		 		  		 		  		 		  		 		  SELECT * 
                            FROM CA_ALL_PRIMARY_KEY
                            EXCEPT 
                            SELECT * 
                            FROM SRC_CHECKED_OUT_PRIMARY_KEY;
                            



INSERT INTO ADD_PRIMARY_KEY_IN_CEM_CAE 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT s1.itm_primary_key
                                       FROM ADD_PRIMARY_KEY s1,
                                            itm s2,
                                            coa
                                       WHERE s2.itm_primary_key = s1.itm_primary_key
                                             AND s2.itm_next_version_id = 999999999
                                             AND s2.itm_container_id = COA_DST_CONTAINER_ID
                                             AND COA_ID = 4
                                             AND EXISTS(SELECT 1
                                                        FROM cem,
                                                             cae,
                                                             GOOD_EVENT_ITEM_ID
                                                        WHERE cem_ca_entry_id = s2.itm_id
                                                              AND cem_ca_entry_id = cae_entry_id
                                                              AND cem_ca_entry_id = GOOD_EVENT_ITEM_ID.wee_entry_id);
                                                              



INSERT INTO ADD_PRIMARY_KEY_NOT_IN_CEM_CAE 
		 		  		 		  		 		  		 		  		 		  		 		  SELECT * 
                                           FROM ADD_PRIMARY_KEY
                                           EXCEPT 
                                           SELECT * 
                                           FROM ADD_PRIMARY_KEY_IN_CEM_CAE;
                                           



INSERT INTO ADD_PRIMARY_KEY_EVENT_ONLY 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT s1.itm_primary_key
                                       FROM coa,
                                            itm s1,
                                            ADD_PRIMARY_KEY_NOT_IN_CEM_CAE,
                                            GOOD_EVENT_ITEM_ID,
                                            NEW_EVENT_ITEM_ID
                                       WHERE COA_ID = 4
                                             AND s1.itm_primary_key = ADD_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
                                             AND s1.itm_next_version_id = 999999999
                                             AND s1.itm_id = GOOD_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_id = NEW_EVENT_ITEM_ID.wee_entry_id
                                             AND s1.itm_container_id = COA_DST_CONTAINER_ID
                                             AND (NOT EXISTS(SELECT 1
                                                             FROM cae
                                                             WHERE cae_entry_id = s1.itm_id))
                                             AND (EXISTS(SELECT 1
                                                         FROM cem
                                                         WHERE cem_ca_entry_id = s1.itm_id));
                                                         




INSERT INTO ADD_PRIMARY_KEY_OK_CEL 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT s1.itm_primary_key
                                   FROM ADD_PRIMARY_KEY s1,
                                        itm s2
                                   WHERE s2.itm_primary_key = s1.itm_primary_key
                                         AND ((NOT EXISTS(SELECT 1
                                                          FROM cel
                                                          WHERE s2.itm_id = cel_entry_id))
                                              OR (EXISTS(SELECT 1
                                                         FROM cel,
                                                              cae
                                                         WHERE s2.itm_id = cel_entry_id
                                                               AND s2.itm_id = cae_entry_id
                                                               AND cae_lock_id = cel_id)));
                                                               


INSERT INTO ADD_GOOD_PRIMARY_KEY 
		 		 (SELECT * 
                                 FROM ADD_PRIMARY_KEY_IN_CEM_CAE
                                 UNION 
                                 SELECT * 
                                 FROM ADD_PRIMARY_KEY_EVENT_ONLY)
                                 INTERSECT 
                                 SELECT * 
                                 FROM ADD_PRIMARY_KEY_OK_CEL;
                                 



INSERT INTO ADD_BAD_PRIMARY_KEY 
		 		  		 		  		 		  		 		  SELECT * 
                                FROM ADD_PRIMARY_KEY
                                EXCEPT 
                                SELECT * 
                                FROM ADD_GOOD_PRIMARY_KEY;
                                



INSERT INTO IN_INITIAL_PRIMARY_KEY 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT itm_primary_key
                                   FROM itm,
                                        cae,
                                        coa,
                                        wfd,
                                        spt
                                   WHERE CAE_ENTRY_ID = itm_id
                                         AND itm_next_version_id = 999999999
                                         AND coa_wfl_id = wfd_id
                                         AND coa_wfl_id = spt_leaf_wfl_id
                                         AND cae_step_path_id = spt_id
                                         AND spt_leaf_step_text_path LIKE 'Initial'
                                         AND CAE_CA_ID = 4;
                                         



INSERT INTO CA_ITM_NO_ITA_PRIMARY_KEY 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT itm_primary_key
                                      FROM itm,
                                           coa
                                      WHERE itm_container_id = COA_DST_CONTAINER_ID
                                            AND itm_next_version_id = 999999999
                                            AND COA_ID = 4
                                            AND NOT EXISTS(SELECT 1
                                                           FROM ita
                                                           WHERE ITA_ITEM_ID = itm_id
                                                                 AND ita_next_version_id = 999999999);
                                                                 




INSERT INTO CAE_ORPHAN_ITEM_ID 
		 		  		 		  		 		  		 		  SELECT DISTINCT cae_entry_id
                               FROM cae
                               WHERE cae_ca_id = 4
                                     AND NOT EXISTS(SELECT 1
                                                    FROM itm,
                                                         coa
                                                    WHERE itm_next_version_id = 999999999
                                                          AND COA_ID = 4
                                                          AND itm_container_id = COA_DST_CONTAINER_ID
                                                          AND itm_id = cae_entry_id);
                                                          



INSERT INTO CEM_SRC_ORPHAN_ITEM_ID 
		 		  		 		  		 		  		 		  SELECT DISTINCT cem_src_entry_id
                                   FROM cem
                                   WHERE cem_ca_id = 4
                                         AND cem_src_entry_id > 0
                                         AND NOT EXISTS(SELECT 1
                                                        FROM itm,
                                                             coa
                                                        WHERE itm_next_version_id = 999999999
                                                              AND COA_ID = 4
                                                              AND itm_container_id = COA_SRC_CONTAINER_ID
                                                              AND itm_id = cem_src_entry_id);
                                                              



INSERT INTO CEM_CA_ORPHAN_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT cem_ca_entry_id
                                  FROM cem
                                  WHERE cem_ca_id = 4
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = cem_ca_entry_id);
                                                             



INSERT INTO CEL_CA_ORPHAN_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT cel_entry_id
                                  FROM cel
                                  WHERE cel_container_id = 4
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = cel_entry_id);
                                                             


INSERT INTO WEE_CA_ORPHAN_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT wee_entry_id
                                  FROM wee,
                                       wfe
                                  WHERE wfe_collaboration_area_id = 4
                                        AND wfe_id = wee_event_id
                                        AND (wfe_event_status = 'NEW'
                                             OR wfe_event_status = 'PROCESSING'
                                             OR wfe_event_status = 'ERROR')
                                        AND NOT EXISTS(SELECT 1
                                                       FROM itm,
                                                            coa
                                                       WHERE itm_next_version_id = 999999999
                                                             AND COA_ID = 4
                                                             AND itm_container_id = COA_DST_CONTAINER_ID
                                                             AND itm_id = wee_entry_id);
                                                             



INSERT INTO CA_ITA_NO_ITM 
		 		  		 		  		 		  		 		  SELECT DISTINCT ita_item_id
                          FROM ita,
                               coa
                          WHERE COA_ID = 4
                                AND ita_catalog_id = COA_DST_CONTAINER_ID
                                AND ita_next_version_id = 999999999
                                AND NOT EXISTS(SELECT 1
                                               FROM itm
                                               WHERE ITA_ITEM_ID = itm_id
                                                     AND itm_next_version_id = 999999999);
                                                     



INSERT INTO CA_ITM_NO_ITA 
		 		  		 		  		 		  		 		  SELECT DISTINCT itm_id
                          FROM itm,
                               coa
                          WHERE COA_ID = 4
                                AND itm_container_id = COA_DST_CONTAINER_ID
                                AND itm_next_version_id = 999999999
                                AND NOT EXISTS(SELECT 1
                                               FROM ita
                                               WHERE ITA_ITEM_ID = itm_id
                                                     AND ita_next_version_id = 999999999);
                                                     



INSERT INTO TOTAL_BAD_PRIMARY_KEY 
		 		  		 		  		 		  		 		  		 		  SELECT * 
                                  FROM IN_INITIAL_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM SRC_BAD_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM ADD_BAD_PRIMARY_KEY
                                  UNION 
                                  SELECT * 
                                  FROM CA_ITM_NO_ITA_PRIMARY_KEY;
                                  



INSERT INTO TOTAL_BAD_ITEM_EVTQ 
		 		  		 		  		 		  		 		  SELECT * 
                                FROM ERROR_EVENT_ITEM_ID
                                UNION 
                                SELECT * 
                                FROM PROCESSING_EVENT_ITEM_ID
                                UNION 
                                SELECT * 
                                FROM WEE_CA_ORPHAN_ITEM_ID;
                                



INSERT INTO ADDITIONAL_BAD_CA_ITEM_ID (ABCII_ITM_ID) 
		 		  		 		  		 		  		 		  		 		  SELECT * 
                                      FROM CAE_ORPHAN_ITEM_ID
                                      UNION 
                                      SELECT * 
                                      FROM CEM_CA_ORPHAN_ITEM_ID
                                      UNION 
                                      SELECT * 
                                      FROM TOTAL_BAD_ITEM_EVTQ
                                      UNION 
                                      SELECT * 
                                      FROM CA_ITA_NO_ITM
                                      UNION 
                                      SELECT * 
                                      FROM CA_ITM_NO_ITA;
                                      



INSERT INTO ADDITIONAL_BAD_SRC_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT * 
                                       FROM CEM_SRC_ORPHAN_ITEM_ID;
                                       



INSERT INTO TOTAL_BAD_CA_ITEM_ID (TBCII_ITM_ID) 
		 		  		 		  		 		  		 		  SELECT DISTINCT x.itm_id
                                 FROM itm x,
                                      TOTAL_BAD_PRIMARY_KEY y,
                                      coa
                                 WHERE x.itm_primary_key = y.itm_primary_key
                                       AND coa_id = 4
                                       AND x.itm_container_id = coa_dst_container_id
                                       AND x.itm_next_version_id = 999999999
                                 UNION 
                                 SELECT * 
                                 FROM ADDITIONAL_BAD_CA_ITEM_ID;
                                 


INSERT INTO TOTAL_BAD_SRC_ITEM_ID (TBSII_ITM_ID) 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT x.itm_id
                                  FROM itm x,
                                       TOTAL_BAD_PRIMARY_KEY y,
                                       coa
                                  WHERE x.itm_primary_key = y.itm_primary_key
                                        AND coa_id = 4
                                        AND x.itm_container_id = coa_src_container_id
                                        AND x.itm_next_version_id = 999999999
                                  UNION 
                                  SELECT * 
                                  FROM CEM_SRC_ORPHAN_ITEM_ID;
                                  



INSERT INTO TOTAL_GOOD_PRIMARY_KEY 
		 		   (SELECT * 
                                   FROM ADD_GOOD_PRIMARY_KEY
                                   UNION 
                                   SELECT * 
                                   FROM SRC_GOOD_PRIMARY_KEY)
                                   EXCEPT 
                                   SELECT * 
                                   FROM TOTAL_BAD_PRIMARY_KEY;
                                   



INSERT INTO TOTAL_GOOD_CA_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT x.itm_id
                                  FROM itm x,
                                       TOTAL_GOOD_PRIMARY_KEY y,
                                       coa
                                  WHERE x.itm_primary_key = y.itm_primary_key
                                        AND coa_id = 4
                                        AND x.itm_container_id = coa_dst_container_id
                                        AND x.itm_next_version_id = 999999999;
                                        



INSERT INTO TOTAL_GOOD_SRC_ITEM_ID 
		 		  		 		  		 		  		 		  		 		  SELECT DISTINCT x.itm_id
                                   FROM itm x,
                                        TOTAL_GOOD_PRIMARY_KEY y,
                                        coa
                                   WHERE x.itm_primary_key = y.itm_primary_key
                                         AND coa_id = 4
                                         AND x.itm_container_id = coa_src_container_id
                                         AND x.itm_next_version_id = 999999999;

COMMIT;
