----- CHI -----
create table temp_chi like tctg_chi_category_hierarchy;

insert into temp_chi
select * from chi
where chi_company_id is null
or chi_parent_id is null
or chi_child_id is null
or chi_next_version_id is null;

insert into temp_chi
select * from chi o where exists
(select 1 from (
select chi_company_id cmp, chi_parent_id prn, chi_child_id chd, chi_next_version_id nxt, count(1) from chi i
group by chi_company_id, chi_parent_id, chi_child_id, chi_next_version_id
having count(1) > 1) as a 
where o.chi_company_id = cmp
and o.chi_parent_id = prn
and o.chi_child_id = chd
and o.chi_next_version_id = nxt
);

delete from chi
where chi_company_id is null
or chi_parent_id is null
or chi_child_id is null
or chi_next_version_id is null;

delete from chi o where exists
(select 1 from (
select chi_company_id cmp, chi_parent_id prn, chi_child_id chd, chi_next_version_id nxt, count(1) from chi i
group by chi_company_id, chi_parent_id, chi_child_id, chi_next_version_id
having count(1) > 1) as a
where o.chi_company_id = cmp
and o.chi_parent_id = prn
and o.chi_child_id = chd
and o.chi_next_version_id = nxt
);
commit;

----- CAT -----
create table temp_cat like tctg_cat_category;

insert into temp_cat
select * from cat
where cat_id is null
or cat_next_version_id is null
or cat_container_id is null
or cat_company_id is null;

insert into temp_cat
select * from cat o where exists
(select 1 from (
select cat_id id, cat_next_version_id nxt, cat_container_id cont, cat_company_id cmp from cat i
group by cat_id, cat_next_version_id, cat_container_id, cat_company_id
having count(1) > 1) as a 
where o.cat_id = id and o.cat_next_version_id = nxt and o.cat_container_id = cont and o.cat_company_id = cmp );

delete from cat
where cat_id is null
or cat_next_version_id is null
or cat_container_id is null
or cat_company_id is null;

delete from cat o where exists
(select 1 from (
select cat_id id, cat_next_version_id nxt, cat_container_id cont, cat_company_id cmp from cat i
group by cat_id, cat_next_version_id, cat_container_id, cat_company_id
having count(1) > 1) as a 
where o.cat_id = id and o.cat_next_version_id = nxt and o.cat_container_id = cont and o.cat_company_id = cmp );
commit;

----- ITM -----
create table temp_itm like tctg_itm_item;

insert into temp_itm
select * from itm
where itm_id is null
or itm_company_id is null;

insert into temp_itm
select * from itm o where exists
(select 1 from (
select itm_id id, itm_next_version_id nxt, itm_container_id cont, itm_company_id cmp from itm
group by itm_id, itm_next_version_id, itm_container_id, itm_company_id
having count(1) > 1) as a 
where o.itm_id = id and o.itm_next_version_id = nxt and o.itm_container_id = cont and o.itm_company_id = cmp );

delete from itm
where itm_id is null
or itm_company_id is null;

delete from itm o where exists
(select 1 from (
select itm_id id, itm_next_version_id nxt, itm_container_id cont, itm_company_id cmp from itm
group by itm_id, itm_next_version_id, itm_container_id, itm_company_id
having count(1) > 1) as a 
where o.itm_id = id and o.itm_next_version_id = nxt and o.itm_container_id = cont and o.itm_company_id = cmp );
commit;

----- ICM -----
create table temp_icm like tctg_icm_item_category_map;

insert into temp_icm
select * from icm
where icm_item_id is null
or icm_next_version_id is null
or icm_cat_tree_id is null
or icm_category_id is null
or icm_company_id is null;

insert into temp_icm
select * from icm o where exists
(select 1 from (
select icm_item_id itm, icm_next_version_id nxt, icm_cat_tree_id ctr, icm_category_id cat, icm_company_id cmp from icm
group by icm_item_id, icm_next_version_id, icm_cat_tree_id, icm_category_id, icm_company_id
having count(1) > 1) as a
where o.icm_item_id = itm and o.icm_next_version_id = nxt
and o.icm_cat_tree_id = ctr and icm_category_id = cat and icm_company_id = cmp  );

delete from icm
where icm_item_id is null
or icm_next_version_id is null
or icm_cat_tree_id is null
or icm_category_id is null
or icm_company_id is null;

delete from icm o where exists
(select 1 from (
select icm_item_id itm, icm_next_version_id nxt, icm_cat_tree_id ctr, icm_category_id cat, icm_company_id cmp from icm
group by icm_item_id, icm_next_version_id, icm_cat_tree_id, icm_category_id, icm_company_id
having count(1) > 1) as a
where o.icm_item_id = itm and o.icm_next_version_id = nxt
and o.icm_cat_tree_id = ctr and icm_category_id = cat and icm_company_id = cmp  );
commit;

----- CFP -----
create table temp_cfp like tctg_cfp_cat_full_paths;

insert into temp_cfp
select * from cfp
where cfp_full_path is null
or cfp_full_path_type is null 
or cfp_next_version_id is null
or cfp_cat_tree_id is null;

insert into temp_cfp
select * from cfp o where exists
(select 1 from (
select cfp_full_path fp, cfp_full_path_type fpt, cfp_next_version_id nxt, cfp_cat_tree_id ctr from cfp
group by cfp_full_path, cfp_full_path_type, cfp_next_version_id, cfp_cat_tree_id
having count(1) > 1) as a 
where o.cfp_full_path = fp and o.cfp_next_version_id = nxt and cfp_cat_tree_id = ctr and o.cfp_full_path_type = fpt);

delete from cfp
where cfp_full_path is null
or cfp_full_path_type is null 
or cfp_next_version_id is null
or cfp_cat_tree_id is null;

delete from cfp o where exists
(select 1 from (
select cfp_full_path fp, cfp_full_path_type fpt, cfp_next_version_id nxt, cfp_cat_tree_id ctr from cfp
group by cfp_full_path, cfp_full_path_type, cfp_next_version_id, cfp_cat_tree_id
having count(1) > 1) as a 
where o.cfp_full_path = fp and o.cfp_next_version_id = nxt and cfp_cat_tree_id = ctr and o.cfp_full_path_type = fpt);
commit;

----- NOH -----
create table temp_noh like tctg_noh_node_hierarchy;

insert into temp_noh
select * from noh
where noh_spec_id is null
or noh_next_version_id is null
or noh_order is null
or noh_company_id is null;

insert into temp_noh
select * from noh o where exists
(select 1 from (
select noh_spec_id spc, noh_next_version_id nxt, noh_order ord, noh_company_id cmp from noh
group by noh_spec_id, noh_next_version_id, noh_order, noh_company_id
having count(1) > 1) as a
where noh_spec_id = spc and noh_next_version_id = nxt and noh_order = ord and noh_company_id = cmp);

delete from noh
where noh_spec_id is null
or noh_next_version_id is null
or noh_order is null
or noh_company_id is null;

delete from noh o where exists
(select 1 from (
select noh_spec_id spc, noh_next_version_id nxt, noh_order ord, noh_company_id cmp from noh
group by noh_spec_id, noh_next_version_id, noh_order, noh_company_id
having count(1) > 1) as a
where noh_spec_id = spc and noh_next_version_id = nxt and noh_order = ord and noh_company_id = cmp);
commit;

----- NOD -----
create table temp_nod like tctg_nod_node;

insert into temp_nod
select * from nod
where nod_id is null
or nod_next_version_id is null
or nod_spec_id is null
or nod_company_id is null;

insert into temp_nod
select * from nod o where exists
(select 1 from (
select nod_id id, nod_next_version_id nxt, nod_spec_id spc, nod_company_id cmp from nod
group by nod_id, nod_next_version_id, nod_spec_id, nod_company_id
having count(1) > 1) as a
where nod_spec_id = spc and nod_next_version_id = nxt and nod_id = id and nod_company_id = cmp);

delete from nod
where nod_id is null
or nod_next_version_id is null
or nod_spec_id is null
or nod_company_id is null;

delete from nod o where exists
(select 1 from (
select nod_id id, nod_next_version_id nxt, nod_spec_id spc, nod_company_id cmp from nod
group by nod_id, nod_next_version_id, nod_spec_id, nod_company_id
having count(1) > 1) as a
where nod_spec_id = spc and nod_next_version_id = nxt and nod_id = id and nod_company_id = cmp);
commit;

----- WFE -----
create table temp_wfe like twfl_wfe_workflow_event;

insert into temp_wfe
select * from wfe
where wfe_id is null;

insert into temp_wfe
select * from wfe o where exists
(select 1 from (
select wfe_id id from wfe
group by wfe_id
having count(1) > 1) as a
where wfe_id = id);

delete from wfe
where wfe_id is null;

delete from wfe o where exists
(select 1 from (
select wfe_id id from wfe
group by wfe_id
having count(1) > 1) as a
where wfe_id = id);

----- WEE -----
create table temp_wee like twfl_wee_workflow_event_entry;

insert into temp_wee
select * from wee
where not exists 
(select 1 from wfe
where wfe_id = wee_event_id);

delete from wee
where not exists 
(select 1 from wfe
where wfe_id = wee_event_id);
