str:='select count(*) from '||tab_inst_trans||' where not inst_ante in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl inst_ante (aus inst_trans) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_inst_trans||' where not inst_ante in (select kb_inst_id from '||tab_inst||')';
str:='select count(*) from '||tab_inst_trans||' where not inst_post in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl inst_post (aus inst_trans) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_inst_trans||' where not inst_post in (select kb_inst_id from '||tab_inst||')';
str:='select count(*) from '||tab_addr_inst_sec_s||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_inst_id (aus addr_inst_s) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_addr_inst_sec_s||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
str:='select count(*) from '||tab_addr_inst_sec_a||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_inst_id (aus addr_inst_a) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_addr_inst_sec_a||' where not kb_inst_id in (select
kb_inst_id from '||tab_inst||')';
commit;
str:='select count(*) from '||tab_a_inst_sec||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_inst_id (aus a_inst_sec) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_a_inst_sec||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
str:='select count(*) from '||tab_s_inst_sec||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_inst_id (aus s_inst_sec) nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_s_inst_sec||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||')';
str:='select count(*) from '||tab_a_inst_sec||' where not kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_sector_id (aus a_inst_sec) nicht in kb_sectors'', count(*), 0, '''||str||'''
from '||tab_a_inst_sec||' where not kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
str:='select count(*) from '||tab_s_inst_sec||' where not kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl kb_sector_id (aus s_inst_sec) nicht in kb_sectors'', count(*), 0, '''||str||'''
from '||tab_s_inst_sec||' where not kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
str:='select count(*) from '||tab_addr_inst_sec_s||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||') and not kb_inst_id=3987 and not kb_inst_id=3830';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl fk_inst aus addr_inst_s nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_addr_inst_sec_s||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||') and not kb_inst_id=3987 and not kb_inst_id=3830';
str:='select count(*) from '||tab_addr_inst_sec_a||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||') and not kb_inst_id=3987 and not kb_inst_id=3830';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'', ''Anzahl fk_inst aus addr_inst_a nicht in kb_inst'', count(*), 0, '''||str||'''
from '||tab_addr_inst_sec_a||' where not kb_inst_id in (select kb_inst_id from '||tab_inst||') and not kb_inst_id=3987 and not kb_inst_id=3830';
commit;
str:='select count(distinct(xxx.single_sector_id) from (select unnest(kb_sector_id) as single_kb_sector_id
from '||tab_addr_inst_sec_s||') as xxx
where not single_kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'',
''Anzahl distinkter kb_sector_id aus addr_inst_sec_s nicht in kb_sector'',
count(distinct(xxx.single_kb_sector_id)),
0,
'''||str||'''
from (select unnest(kb_sector_id) as single_kb_sector_id
from '||tab_addr_inst_sec_s||') as xxx
where not single_kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
commit;
str:='select count(distinct(xxx.single_sector_id) from (select unnest(kb_sector_id) as single_kb_sector_id
from '||tab_addr_inst_sec_a||') as xxx
where not single_kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
EXECUTE 'insert into '||ergebnis_tab_name||' select ''Constraints Check'',
''Anzahl distinkter kb_sector_id aus addr_inst_sec_s nicht in kb_sector'',
count(distinct(xxx.single_kb_sector_id)),
0,
'''||str||'''
from (select unnest(kb_sector_id) as single_kb_sector_id
from '||tab_addr_inst_sec_a||') as xxx
where not single_kb_sector_id in (select kb_sector_id from '||tab_sectors||')';
commit;
/*** Create sampling tables ***/
EXECUTE 'drop table if exists '||name_stichprobentab_1;
EXECUTE 'create table '||name_stichprobentab_1||' as (
select * from (select * from '||tab_addr_inst_sec_s||'
order by random()) as xyz limit 100)';
commit;
EXECUTE 'insert into '||ergebnis_tab_name||' values (''Name Stichprobentabelle 1 (Zufallsauswahl 100 Zuordnungen Modus S)'','''||name_stichprobentab_1||''',null, null, null)';
commit;
execute 'drop table if exists '||name_stichprobentab_2||'_tmp';
execute 'create index on '||tab_addr_inst_sec_s||'('||adr_spalte||')';
execute 'create table '||name_stichprobentab_2||'_tmp as (
select a.'||adr_spalte||'
from '||basis_tab||' a
left join '||tab_addr_inst_sec_s||' b
on a.'||adr_spalte||' = b.'||adr_spalte||'
where b.'||adr_spalte||' is null)';
commit;
execute 'drop table if exists '||name_stichprobentab_2||'_tmp_1';
execute 'create table '||name_stichprobentab_2||'_tmp_1 as (
select distinct(upper('||adr_spalte||')) as '||adr_spalte||'
from '||name_stichprobentab_2||'_tmp)';
commit;
execute 'drop index if exists processing.i_check_kodierung_upper_adr';
execute 'create index i_check_kodierung_upper_adr on '||tab_addr_inst_sec_s||'(upper('||adr_spalte||'))';
execute 'delete from '||name_stichprobentab_2||'_tmp_1
where '||adr_spalte||' in (select upper('||adr_spalte||') from '||tab_addr_inst_sec_s||')';
commit;
EXECUTE 'drop table if exists '||name_stichprobentab_2;
EXECUTE 'create table '||name_stichprobentab_2||' as (