

truncate table promstat_docprogram_aggr;

insert into promstat_docprogram_aggr( person_id,
       degree_program_id,
       doctoral_program_id,
       sem_rueck_beur_ein,
       examplan_id,
       course_of_study_id,
       astat_bund,
       astat_land,
       k_type_of_doctorate,
       gueltig_von,
       gueltig_bis,
       finished,
       structured_doctoral_program_id,
       stu_employment_business_id,
       stu_employment_startdate,
       stu_employment_enddate,
       k_examform_id,
       k_examform_un,
       k_place_of_studies_astat,
       geschlecht_astat,
       staatsangehoerigkeit_1_astat,
       staatsangehoerigkeit_2_astat,
       hzb_datum,
       hzb_jahr,
       hzb_type_astat,
       hzb_kreis_astat,
       hzb_staat_astat,
       first_external_semester,
       first_external_year,
       first_external_country_astat,
       first_external_university_astat,
       summe
       )
       SELECT P.his_person_id,
P.degree_program_id,
P.doctoral_program_id,
P.sem_rueck_beur_ein,
P.examplan_id,
P.course_of_study_id,
substring(P.astat_bund from 1 for 10),
P.astat_land,
P.k_type_of_doctorate,
P.gueltig_von,
P.gueltig_bis,
P.finished,
P.structured_doctoral_program_id,
P.stu_employment_business_id,
P.stu_employment_startdate,
P.stu_employment_enddate,
P.k_examform_id,
P.k_examform_un,
P.k_place_of_studies_astat,
D.geschlecht_astat,
D.staatsangehoerigkeit_1_astat,
D.staatsangehoerigkeit_2_astat,
D.hzb_datum,
year(D.hzb_datum),
substring(D.hzb_type_astat from 1 for 10),
val(D.hzb_kreis_astat),
val(D.hzb_staat_astat),
D.first_external_semester,
val(substring('' || D.first_external_semester from 1 for 4)) as first_external_year,
val(D.first_external_country_astat),
substring(first_external_university_astat from 1 for 10),
count(*)
 
FROM prom_promotionen P left outer join  prom_promovierende D
on (P.his_person_id=D.his_person_id)
group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
 ;
 
update promstat_docprogram_aggr set prev_exam_university_astat=A.ext_university_astat,
prev_exam_country_astat=val(ext_country_astat),
prev_exam_degree_land=substring(ext_degree_land_astat from 1 for 10),
prev_exam_degree_bund=substring(ext_degree_bund_astat from 1 for 10)
from prom_abschlusspruefungen A
where A.examplan_id=promstat_docprogram_aggr.examplan_id
;
 
update promstat_docprogram_aggr set prev_exam_semester=E.sem_der_pruefung
from lm_examplan E
where E.id=promstat_docprogram_aggr.examplan_id;

--nur wenn lm_examplan nichts findet:

update promstat_docprogram_aggr set prev_exam_semester=S.tid
from prom_abschlusspruefungen A, semester S
where promstat_docprogram_aggr.prev_exam_semester is null
and A.examplan_id=promstat_docprogram_aggr.examplan_id
and A.examination_date between S.sem_beginn and S.sem_ende
;

update promstat_docprogram_aggr set prev_exam_year=val(substring('' || prev_exam_semester from 1 for 4))
;

update promstat_docprogram_aggr set 
prev_exam_degree_land=substring(int_degree_land_astat from 1 for 10)
from prom_abschlusspruefungen A
where A.examplan_id=promstat_docprogram_aggr.examplan_id
and promstat_docprogram_aggr.prev_exam_degree_land is null
;

update promstat_docprogram_aggr set 
prev_exam_degree_bund=substring(int_degree_bund_astat from 1 for 10)
from prom_abschlusspruefungen A
where A.examplan_id=promstat_docprogram_aggr.examplan_id
and promstat_docprogram_aggr.prev_exam_degree_bund is null
;
 
-- update promstat_docprogram_aggr set abschluss_astat_bund=substring(abschluss_astat_bund from 3 for 2)
-- where length(abschluss_astat_bund)=4;
-- 
-- update promstat_docprogram_aggr set abschluss_astat_bund=substring(abschluss_astat_bund from 2 for 2)
-- where length(abschluss_astat_bund)=3;
-- 

