--freemarker template
<sqlvars>
<sqlvar name="beginn_semester"><![CDATA[
select param_val from unload_params
where param_id='COSTAGE_start_st_sem';
]]>
</sqlvar>
</sqlvars>

drop table if exists tmp_studien;
drop table if exists tmp_studien2;
drop table if exists tmp_tsg2msg;

SELECT
SM.semester_tid as semester,
SM.semester_anfang,
SM.semester_ende,
SG.st_absz_nr,
SG.st_skz_nr,
SG.studiengangs_identifikator,
SG.studiengangs_bezeichnung,
SM.st_sem_nr,
ST.st_studium_nr,
S.st_studstamm_nr,
ST.st_studiengang_nr,
SG.studiengangs_typ_kb,
SA.studienstatustyp,
SA.st_studienstatus_nr,
SA.beginn_datum,
SA.gueltig_ab,
val(S.matrikelnummer) as matrikelnummer,
S.geschlecht,
S.ca12_staat,
S.second_nationality,
SG.fachkennzeichen,
ST.studienform_statistik_code,
--(case when SG.studiengangs_typ_kb in ('MSG') then SG.st_studiengang_nr else null::integer end) as st_studiengang_nr_msg,
null::varchar(255) as studiengangs_identifikator_msg,
null::integer as st_studiengang_nr_msg_primaerflag,
--(case when SG.studiengangs_typ_kb in ('TSG') then SG.st_studiengang_nr else null::integer end) as st_studiengang_nr_tsg,
--null::integer as studienfach_sortierung,
null::char(10) as hrst,
null::integer as fachsemester,
null::integer as praxissemester,
null::integer as hssem,
null::integer as klinsem,
(case when SG.studiengangs_typ_kb in ('TSG') then SA.studienstatustyp else null::char(10) end) as statustyp_tsg,
(case when ST.hauptstudium_flag='J' then 1::smallint else 0::smallint end) as primaerflag_studien,
0::smallint as tsg_generated,
--0::smallint as primaerflag,
null::integer as studiengang_nr,
null::integer as fach_nr,
null::char(10) as unikey,
null::date as anfdat,
null::date as ruebeudat,
null::date as endedat,
null::smallint as kz_rueck_beur_ein,
null::smallint as kz_rueck_beur_ein_tsg,
null::integer as hzb_art_nr,
null::integer as hzb_land_nr,
null::integer as hzb_land_sub_r_nr ,
null::integer as hzb_jahr,
null::date as hzb_datum ,
null::decimal(14,7) as hzb_note,
null::char(10) as abm_art_s, 
rank () over (partition by SM.st_sem_nr, S.st_studstamm_nr, SG.studiengangs_identifikator  order by SA.gueltig_ab desc,SA.studienstatustyp desc) as status_historie
--rank () over (partition by SM.st_sem_nr, S.st_studstamm_nr, SG.studiengangs_identifikator order by (case when SA.gueltig_ab =SM.semester_ende then date_val('01.01.1900') else SA.gueltig_ab end) desc) as status_historie
into temp tmp_studien
FROM costage_st_studierendenstammdaten S,
 costage_st_studien ST,
 costage_st_semester SM,
 costage_st_studiengaenge SG ,
 costage_st_studienstatus SA
 

where
S.st_studstamm_nr = ST.st_studstamm_nr
and ST.st_studiengang_nr=SG.st_studiengang_nr
and S.matrikelnummer is not null
--and SM.semester_tid  >= 20171
-- and SM.semester_tid <= 20171
and SG.studiengangs_typ_kb in ('ESG','TSG','MSG')
and SA.st_studium_nr=ST.st_studium_nr
and SA.st_sem_nr=SM.st_sem_nr
and SA.gueltig_ab <=SM.semester_ende --nur letzter Status im jew. Semester interessiert, nicht danach
and (SA.studienstatustyp not in ('Z','X') or (
	SA.studienstatustyp  in ('Z','X') 
	and SA.gueltig_ab <= today()
	and date(SA.gueltig_ab) < date(SM.semester_ende)
	)) --Studis außer erst/neueinschr/rückmeld/beurlaubt werden in der Semestermitte exmatr. zum Endedatum Semesterende, wenn dieses Datum in der Zukunft liegt soll der Studi mit dem vorherigen Status erscheinen
and SA.studienstatustyp not in ('a','o','z')
--group by 1,2,3,4,5,6,7,8,9,10,11,12,13
and ${COSTAGE_STUDENT_FILTER}
and SM.semester_tid >= ${beginn_semester}
;

select 1,timestamp_str(now()) from xdummy;

update tmp_studien
set hrst=H.hoererstatus_kb
from costage_st_hoererstatus H
where tmp_studien.st_studstamm_nr = H.st_studstamm_nr
and tmp_studien.st_sem_nr = H.st_sem_nr
and hoererstatus_kb != 'E' --der Hörerstatus "E - Exmatrikuliert" gilt eigentlich erst zum Studienende, hat aber kein "gültig ab" Datum. Der Hörerstatus "E" kann ignoriert werden. 
;

--tmp_studien enthält TSG, ESG und MSG
--zuerst die msg:
drop table if exists tmp_studien_msg;

select 
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_studium_nr,
st_studstamm_nr,
st_studiengang_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
semester,
primaerflag_studien,
hrst,
null::char(10) as abm_art_s
 into temp tmp_studien_msg 
from tmp_studien T where T.studiengangs_typ_kb in ('MSG')
and T.status_historie=1
;

select 10,timestamp_str(now()) from xdummy;

--Abmeldungsart studienbezogen:
update tmp_studien_msg set abm_art_s=P.st_abm_art_kurzbezeichnung
from costage_st_personen_abmeldungen P, costage_st_semester S, costage_st_abmeldungsarten A
where S.st_sem_nr=P.st_sem_nr
and S.semester_tid=tmp_studien_msg.semester
and P.st_studium_nr=tmp_studien_msg.st_studium_nr
and A.st_abm_art_kurzbezeichnung=P.st_abm_art_kurzbezeichnung
and A.bezug='S'
;

--wir machen den MSG von einer Zeile zu einer Spalte
--1. Prio TSG hat Primärflag:
drop table if exists tmp_studien_esg_tsg;
select *,
1::integer as primaerflag,
null::integer as letztes_tsg_semester,
null::integer as st_studium_nr_msg,
null::date as endedat_tsg
 into temp tmp_studien_esg_tsg from tmp_studien T
where T.studiengangs_typ_kb in ('ESG','TSG')
and T.status_historie=1
 and T.primaerflag_studien=1
 ;

--2. Prio: aus costage_st_hauptstudien:
insert into tmp_studien_esg_tsg
select T.*,
(case when H.st_studstamm_nr > 0 then 1::integer else 0::integer end) as primaerflag,
null::integer as letztes_tsg_semester,
null::integer as st_studium_nr_msg
from tmp_studien T left outer join costage_st_hauptstudien H
 on ( H.st_studstamm_nr=T.st_studstamm_nr
 and H.st_studium_nr=T.st_studium_nr
 and H.st_sem_nr=T.st_sem_nr)
 where T.status_historie=1
 and T.studiengangs_typ_kb in ('ESG','TSG')
 and T.primaerflag_studien=0
 ;
 

update tmp_studien_esg_tsg set st_studium_nr_msg=MSG.st_studium_nr
from tmp_studien_msg MSG, costage_st_studiengaenge_mtsg MTSG
where tmp_studien_esg_tsg.studiengangs_typ_kb in ('TSG') 
and tmp_studien_esg_tsg.semester=MSG.semester
 and tmp_studien_esg_tsg.st_studstamm_nr=MSG.st_studstamm_nr
and tmp_studien_esg_tsg.st_studiengang_nr=MTSG.st_studiengang_nr_tsg
 and tmp_studien_esg_tsg.st_absz_nr=MSG.st_absz_nr
and MSG.st_studiengang_nr=MTSG.st_studiengang_nr_msg
and MSG.primaerflag_studien=1
;

select 20,timestamp_str(now()) from xdummy;


update tmp_studien_esg_tsg set st_studium_nr_msg=MSG.st_studium_nr
from tmp_studien_msg MSG, costage_st_studiengaenge_mtsg MTSG
where tmp_studien_esg_tsg.st_studium_nr_msg is null
and tmp_studien_esg_tsg.studiengangs_typ_kb in ('TSG') 
and tmp_studien_esg_tsg.semester=MSG.semester
 and tmp_studien_esg_tsg.st_studstamm_nr=MSG.st_studstamm_nr
and tmp_studien_esg_tsg.st_studiengang_nr=MTSG.st_studiengang_nr_tsg
 and tmp_studien_esg_tsg.st_absz_nr=MSG.st_absz_nr
and MSG.st_studiengang_nr=MTSG.st_studiengang_nr_msg
and MSG.primaerflag_studien=0
;

select 30,timestamp_str(now()) from xdummy;


update tmp_studien_esg_tsg
set fachsemester=F.fachsemester
from costage_st_fachsemester F
where F.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and F.st_sem_nr=tmp_studien_esg_tsg.st_sem_nr
and F.fachsemestertyp='FS'
;

select 35,timestamp_str(now()) from xdummy;


--praxissemester:
update tmp_studien_esg_tsg
set praxissemester=F.fachsemester
from costage_st_fachsemester F
where F.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and F.st_sem_nr=tmp_studien_esg_tsg.st_sem_nr
and F.fachsemestertyp='PS'
;

select 36,timestamp_str(now()) from xdummy;


--klinsem
update tmp_studien_esg_tsg
set klinsem=F.fachsemester
from costage_st_fachsemester F
where F.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and F.st_sem_nr=tmp_studien_esg_tsg.st_sem_nr
and F.fachsemestertyp='KS'
;

select 40,timestamp_str(now()) from xdummy;

update tmp_studien_esg_tsg
set hzb_art_nr=H.hzb_art_nr,
hzb_datum=H.hzb_datum,
hzb_note=(case when substring(H.hzb_note from 1 for 1) ~ '[0-9]'
and substring(H.hzb_note from 2 for 1)  in (',','.')
and substring(H.hzb_note from 3  for 1) ~ '[0-9]'
and (substring(H.hzb_note from 4 for 1) ~ '[0-9]' or substring(H.hzb_note from 4 for 1)='')
and length(H.hzb_note)<=4 then to_number(replace(H.hzb_note,',','.'),'9.99') 
when H.hzb_note ~ '[0-9]'  and length(H.hzb_note) <=4  then decval(H.hzb_note)
else null::decimal(8,4) end ),
hzb_land_sub_r_nr=H.hzb_land_sub_r_nr,
hzb_land_nr=H.hzb_land_nr,
hzb_jahr=H.hzb_jahr
from costage_st_hzb H
where tmp_studien_esg_tsg.st_studstamm_nr = H.st_person_nr
;

select 50,timestamp_str(now()) from xdummy;

--Abmeldungsart studienbezogen:
update tmp_studien_esg_tsg set abm_art_s=P.st_abm_art_kurzbezeichnung
from costage_st_personen_abmeldungen P, costage_st_semester S, costage_st_abmeldungsarten A
where S.st_sem_nr=P.st_sem_nr
and S.semester_tid=tmp_studien_esg_tsg.semester
and P.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and A.st_abm_art_kurzbezeichnung=P.st_abm_art_kurzbezeichnung
and A.bezug='S'
;

select 55,timestamp_str(now()) from xdummy;

--Endedatum bei TSG nachladen bei Abmeldungen:
update tmp_studien_esg_tsg set endedat_tsg=SA.gueltig_ab
from  costage_st_studienstatus SA
where SA.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and SA.st_sem_nr=tmp_studien_esg_tsg.st_sem_nr
and SA.studienstatustyp  in ('Z','X')
and tmp_studien_esg_tsg.abm_art_s is not null
;

select 57,timestamp_str(now()) from xdummy;

update tmp_studien_esg_tsg set endedat=SA.gueltig_ab
from  costage_st_studienstatus SA
where SA.st_studium_nr=tmp_studien_esg_tsg.st_studium_nr
and SA.st_sem_nr=tmp_studien_esg_tsg.st_sem_nr
and SA.studienstatustyp  in ('Z','X')
and tmp_studien_esg_tsg.studiengangs_typ_kb='ESG'
and tmp_studien_esg_tsg.abm_art_s is not null
;

select 60,timestamp_str(now()) from xdummy;


drop table if exists tmp_studien_tsg;


select ETSG.*,  
MSG.primaerflag_studien as primaerflag_studien_msg,
MTSG.st_studiengang_nr_msg,
MTSG.studienfach_sortierung,
MSG.abm_art_s as abm_art_s_msg
into temp tmp_studien_tsg
from tmp_studien_esg_tsg ETSG,tmp_studien_msg MSG,costage_st_studiengaenge_mtsg MTSG
where
 MSG.semester=ETSG.semester
and MSG.st_studium_nr=ETSG.st_studium_nr_msg
and MSG.st_studstamm_nr=ETSG.st_studstamm_nr
and MTSG.st_studiengang_nr_tsg=ETSG.st_studiengang_nr
and MTSG.st_studiengang_nr_msg=MSG.st_studiengang_nr
and ETSG.studiengangs_typ_kb in ('TSG');


create index tmp_ix3 on tmp_studien(studiengangs_typ_kb);
create index tmp_ix5 on tmp_studien(status_historie);
create index tmp_ix6 on tmp_studien(primaerflag_studien);

 
--statustyp bei TSG aus MSG nachladen:
update tmp_studien_tsg set studienstatustyp=S.studienstatustyp,
studiengangs_identifikator_msg=S.studiengangs_identifikator
from tmp_studien_msg S
where tmp_studien_tsg.st_studiengang_nr_msg=S.st_studiengang_nr 
and tmp_studien_tsg.st_studstamm_nr=S.st_studstamm_nr
and tmp_studien_tsg.semester=S.semester
 
;

select 70,timestamp_str(now()) from xdummy;



--select * from tmp_studien
--where semester=20212;


<#if K_COSTAGE_TSG_GENERATE == 1>

--start generierung:
drop table if exists  tmp_studien_msg_tsg;

select  MTSG.st_studiengang_nr_tsg as st_studiengang_nr_tsg,
MSG.st_studstamm_nr,
MSG.st_studiengang_nr as st_studiengang_nr_msg,
min(MSG.semester) as startsemester_msg,
max(MSG.semester)  as endsemester_msg,
max(TSG.semester) as endsemester_tsg
into temp tmp_studien_msg_tsg
 from tmp_studien_msg MSG, costage_st_studiengaenge_mtsg MTSG , tmp_studien_tsg TSG
where MTSG.st_studiengang_nr_msg=MSG.st_studiengang_nr
and TSG.st_studiengang_nr=MTSG.st_studiengang_nr_tsg
and TSG.st_studstamm_nr=MSG.st_studstamm_nr
--and TSG.st_studium_nr_msg=MSG.st_studium_nr
and MSG.studienstatustyp in ('U','B','E','I','X','Z') --nur tatsächlich eingeschrieben;
group by 1,2,3
;

select 90,timestamp_str(now()) from xdummy;


select 100,timestamp_str(now()) from xdummy;

--nun fehlende Datensätze ermitteln:
drop table if exists tmp_studien_msg_tsg_fehlend;

select distinct S.semester_tid as semester,
S.semester_anfang,
S.semester_ende,
S.st_sem_nr,
T.st_studstamm_nr,
T.st_studiengang_nr_tsg,
T.st_studiengang_nr_msg,
T.endsemester_tsg
 into temp tmp_studien_msg_tsg_fehlend
from costage_st_semester S inner join  tmp_studien_msg_tsg T on 
(S.semester_tid between T.startsemester_msg and T.endsemester_msg) --enthält alle möglichen TSG
left outer   join tmp_studien_tsg TSG --enthält die tatsächlich vorhandenen TSG
on (TSG.st_studiengang_nr=T.st_studiengang_nr_tsg
and TSG.st_studstamm_nr=T.st_studstamm_nr
and S.semester_tid=TSG.semester)
where 
TSG.st_studstamm_nr is null
;



drop table if exists tmp_studien_msg_tsg_fehlend2;


--Studiengangsdaten des TSG nachladen:
--Ein Studi kann einen TSG mehrmals schließen (z.B. Beurlaubung)
--daher wird das max. Semester genommen, und der Status dann nachgeladen:
select F.semester,
F.semester_anfang,
F.semester_ende,
 T.st_absz_nr,
 T.st_skz_nr,
 T.studiengangs_identifikator,
 T.studiengangs_bezeichnung,
F.st_sem_nr,
 T.st_studium_nr,
T.st_studstamm_nr,
 T.studiengangs_typ_kb,
 T.matrikelnummer,
 T.geschlecht,
 T.ca12_staat,
 T.second_nationality,
 T.fachkennzeichen,
 T.studienform_statistik_code,
 --T.st_studiengang_nr_msg,
 F.st_studiengang_nr_msg,
-- T.studiengangs_identifikator_msg,
 T.st_studiengang_nr as st_studiengang_nr_tsg,
null::char(10) as  hrst,
null::integer as hssem,
null::char(10) as  studienstatustyp,
null::char(10) as  statustyp_tsg,
1::integer as tsg_generated,
-- T.primaerflag,
 T.hzb_art_nr,
 T.hzb_land_sub_r_nr ,
 T.hzb_jahr,
 T.hzb_datum ,
 T.hzb_note,
 T.hzb_land_nr,
null::char(10) as  abm_art_s,
null::char(10) as  abm_art_s_msg,
T.studienfach_sortierung,
null::timestamp as  beginn_datum,
null::timestamp AS gueltig_ab,
null::integer as st_studienstatus_nr,
null::integer as primaerflag_studien_msg,
null::integer as st_studium_nr_msg,
max(T.semester) as letztes_tsg_semester,
max(T.fachsemester) as fachsemester,
max(T.praxissemester) as praxissemester,
max(T.klinsem) as klinsem
into temp tmp_studien_msg_tsg_fehlend2
from tmp_studien_msg_tsg_fehlend F, tmp_studien_tsg T
where F.st_studstamm_nr=T.st_studstamm_nr
and F.st_studiengang_nr_tsg=T.st_studiengang_nr
and T.semester=F.endsemester_tsg
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,23,24,25,26,27,28,29 ,30 ,31,32 ,33 ,34,35,36,37,38
;

update tmp_studien_msg_tsg_fehlend2 set 
hssem=S.hssem,
statustyp_tsg=S.statustyp_tsg,
abm_art_s=S.abm_art_s,
abm_art_s_msg=S.abm_art_s_msg,
beginn_datum =S.beginn_datum,
gueltig_ab=S.gueltig_ab,
st_studienstatus_nr=S.st_studienstatus_nr,
st_studium_nr_msg=S.st_studium_nr_msg
 from tmp_studien_tsg S
 where  S.st_studiengang_nr=tmp_studien_msg_tsg_fehlend2.st_studiengang_nr_tsg
 and S.st_studstamm_nr=tmp_studien_msg_tsg_fehlend2.st_studstamm_nr
 and S.semester=tmp_studien_msg_tsg_fehlend2.letztes_tsg_semester 
 ;
--Primärflag des MSG ermitteln, wichtig für die Ermittlung der Studiengang-Nr.
update  tmp_studien_msg_tsg_fehlend2 set 
primaerflag_studien_msg=M.primaerflag_studien
from tmp_studien_msg M
where tmp_studien_msg_tsg_fehlend2.st_studiengang_nr_msg=M.st_studiengang_nr 
and tmp_studien_msg_tsg_fehlend2.st_studstamm_nr=M.st_studstamm_nr
and tmp_studien_msg_tsg_fehlend2.semester=M.semester;



--fehlende Semester nachtragen:
insert into tmp_studien_tsg(semester,
semester_anfang,
semester_ende,
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_sem_nr,
st_studium_nr,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
fachkennzeichen,
studienform_statistik_code,
st_studiengang_nr_msg,
studiengangs_identifikator_msg,
st_studiengang_nr,
hrst,
hssem,
statustyp_tsg,
tsg_generated,
primaerflag,
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
abm_art_s_msg,
fachsemester,
praxissemester,
klinsem,
studienfach_sortierung,
letztes_tsg_semester,
primaerflag_studien_msg,
st_studium_nr_msg
)
select distinct semester,
semester_anfang,
semester_ende,
F.st_absz_nr,
F.st_skz_nr,
F.studiengangs_identifikator,
F.studiengangs_bezeichnung,
st_sem_nr,
st_studium_nr,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
F.fachkennzeichen,
studienform_statistik_code,
st_studiengang_nr_msg,
G.studiengangs_identifikator as studiengangs_identifikator_msg,
st_studiengang_nr_tsg,
hrst,
hssem,
'X' as  statustyp_tsg, -- generierte TSG sind immer geschlossen S.statustyp_tsg,
1::integer as tsg_generated,
0 as primaerflag, --geschlossene TSG können kein Primärflag haben
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
abm_art_s_msg,
fachsemester,
praxissemester,
klinsem,
studienfach_sortierung,
letztes_tsg_semester,
primaerflag_studien_msg,
st_studium_nr_msg
from  tmp_studien_msg_tsg_fehlend2 F, costage_st_studiengaenge G
where G.st_studiengang_nr=F.st_studiengang_nr_msg
 ;



select 110,timestamp_str(now()) from xdummy;



--statustyp und hrst nachladen:
 update tmp_studien_tsg set studienstatustyp=S.studienstatustyp,
 hrst=S.hrst
 from tmp_studien_msg S
 where tmp_studien_tsg.st_studiengang_nr_msg=S.st_studiengang_nr 
 and tmp_studien_tsg.st_studstamm_nr=S.st_studstamm_nr
 and tmp_studien_tsg.tsg_generated=1
 and tmp_studien_tsg.semester=S.semester
  ;


--select * from tmp_studien_msg_tsg_fehlend2;


select 120,timestamp_str(now()) from xdummy;


drop table tmp_studien_msg_tsg_fehlend;
drop table tmp_studien_msg_tsg_fehlend2;
drop table tmp_studien_msg_tsg;


</#if>
--------------------------------------------------------------------
--Zusammenführen ESG und TSG:

select 130,timestamp_str(now()) from xdummy;

drop table if exists tmp_studien2;

select 
semester,
semester_anfang,
semester_ende,
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_sem_nr,
st_studium_nr,
st_studium_nr_msg,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
fachkennzeichen,
studienform_statistik_code,
st_studiengang_nr_msg,
studiengangs_identifikator_msg,
st_studiengang_nr,
hrst,
hssem,
statustyp_tsg,
tsg_generated,
primaerflag,
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
abm_art_s_msg,
fachsemester,
praxissemester,
klinsem,
studienfach_sortierung,
letztes_tsg_semester,
primaerflag_studien_msg,
'TSG'::char(10) as studiengangs_typ_kb,
null::integer as fach_nr,
null::integer as studiengang_nr,
null::char(10) as unikey,
null::integer as kz_rueck_beur_ein,
endedat,
endedat_tsg
into temp tmp_studien2
from tmp_studien_tsg;

--nun die ESG:
insert into tmp_studien2(semester,
semester_anfang,
semester_ende,
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_sem_nr,
st_studium_nr,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
fachkennzeichen,
studienform_statistik_code,
st_studiengang_nr_msg,
studiengangs_identifikator_msg,
st_studiengang_nr,
hrst,
hssem,
statustyp_tsg,
tsg_generated,
primaerflag,
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
fachsemester,
praxissemester,
klinsem,
studienfach_sortierung,
letztes_tsg_semester,
studiengangs_typ_kb,
endedat
)
select 
semester,
semester_anfang,
semester_ende,
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_sem_nr,
st_studium_nr,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
fachkennzeichen,
studienform_statistik_code,
null::integer as st_studiengang_nr_msg,
null::varchar(255) as studiengangs_identifikator_msg,
st_studiengang_nr,
hrst,
hssem,
null::char(10) as statustyp_tsg,
0::integer as tsg_generated,
primaerflag,
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
fachsemester,
praxissemester,
klinsem,
1::integer  as studienfach_sortierung,
letztes_tsg_semester,
studiengangs_typ_kb,
endedat
from tmp_studien_esg_tsg E
where E.studiengangs_typ_kb='ESG';
 

drop table tmp_studien;
drop table tmp_studien_tsg;
drop table tmp_studien_esg_tsg;


--Ermittlung fach_nr:
update tmp_studien2 set fach_nr=studienfach_sortierung;


update tmp_studien2 set fach_nr=1
where fach_nr is null;

select 140,timestamp_str(now()) from xdummy;


update tmp_studien2 set unikey=K.unikey
from costage_st_studiengaenge G, costage_st_studienkennzahlen K
where G.st_studiengang_nr=tmp_studien2.st_studiengang_nr
and K.st_skz_nr=G.st_skz_nr
;

update tmp_studien2 set kz_rueck_beur_ein=(select val(astat)
from cifx where key=400 
and apnr=tmp_studien2.studienstatustyp
);


update tmp_studien2
set hssem=H.anzahl_hochschulsemester
from costage_st_hochschulsemester H
where H.st_studstamm_nr=tmp_studien2.st_studstamm_nr
and H.st_sem_nr=tmp_studien2.st_sem_nr
;


update tmp_studien2 set studiengang_nr=1 where primaerflag=1;

drop table if exists tmp_primaer_msg;
--Wenn Studium  mit Primärflag ein TSG ist, wird die studiengang_nr auf alle anderen TSG des MSG ausgedehnt: 
select st_studstamm_nr,semester,st_studiengang_nr_msg
into temp tmp_primaer_msg
from tmp_studien2
where studiengangs_typ_kb ='TSG'
and st_studiengang_nr_msg is not null
--and primaerflag=1
and primaerflag_studien_msg=1
;

--select * from tmp_primaer_msg;

update tmp_studien2 set studiengang_nr=1 where primaerflag=0
and studiengangs_typ_kb ='TSG'
and primaerflag_studien_msg=1
and exists (select st_studiengang_nr_msg
from tmp_primaer_msg T
where T.st_studstamm_nr=tmp_studien2.st_studstamm_nr
and T.semester=tmp_studien2.semester
and T.st_studiengang_nr_msg=tmp_studien2.st_studiengang_nr_msg
)
;
select 150,timestamp_str(now()) from xdummy;

drop table if exists tmp_studien3;

--SPO-Version (letzte im jew. Semester) ermitteln:
select
semester,
semester_anfang,
semester_ende,
st_absz_nr,
st_skz_nr,
studiengangs_identifikator,
studiengangs_bezeichnung,
st_sem_nr,
T.st_studium_nr,
T.st_studium_nr_msg,
st_studstamm_nr,
studienstatustyp,
st_studienstatus_nr,
beginn_datum,
T.gueltig_ab,
matrikelnummer,
geschlecht,
ca12_staat,
second_nationality,
fachkennzeichen,
studienform_statistik_code,
st_studiengang_nr_msg,
studiengangs_identifikator_msg,
st_studiengang_nr,
hrst,
hssem,
statustyp_tsg,
tsg_generated,
primaerflag,
hzb_art_nr,
hzb_land_sub_r_nr ,
hzb_jahr,
hzb_datum ,
hzb_note,
hzb_land_nr,
abm_art_s,
abm_art_s_msg,
fachsemester,
praxissemester,
klinsem,
studienfach_sortierung,
letztes_tsg_semester,
studiengangs_typ_kb,
fach_nr,
studiengang_nr,
unikey,
kz_rueck_beur_ein,
endedat,
endedat_tsg,
null::char(10) as po_stp_stp_version_kb,
null::integer as po_regelstudienzeit,
null::integer as kz_rueck_beur_ein_tsg,
max(SPO.gueltig_ab) as spo_max_gueltig_ab
into temp tmp_studien3
from tmp_studien2 T left outer join costage_st_studien_spoversionen SPO
on (SPO.st_studium_nr=T.st_studium_nr
and SPO.gueltig_ab < T.semester_ende)
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,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45 ,46, 47,48,49 --,50,51,52
;

select 160,timestamp_str(now()) from xdummy;

update tmp_studien3 set po_stp_stp_version_kb= SPO.po_stp_stp_version_kb,
po_regelstudienzeit=SPO.po_regelstudienzeit
from costage_st_studien_spoversionen SPO
where SPO.st_studium_nr=tmp_studien3.st_studium_nr
and tmp_studien3.spo_max_gueltig_ab =SPO.gueltig_ab;

update tmp_studien3 set kz_rueck_beur_ein_tsg=(select val(astat)
from cifx where key=400 
and apnr=tmp_studien3.statustyp_tsg
);

select 170,timestamp_str(now()) from xdummy;


drop table tmp_studien2;


delete from costage_st_studien_aggr
--provisorisch in Testphase:
where ${COSTAGE_STUDENT_FILTER}
and semester >= ${beginn_semester};

create index ix_tmp_studien2 on tmp_studien3(kz_rueck_beur_ein)
;
insert into costage_st_studien_aggr(
st_studium_nr,
st_studium_nr_msg,
       st_studstamm_nr,
       matrikelnummer,
       st_studiengang_nr,
       st_studiengang_nr_msg,
       st_studienstatus_nr,
       statustyp,
       statustyp_tsg,
       tsg_generated,
       hrst,
       semester,
       fachsemester,
       praxissemester,
       klinsem,
       hssem,
       primaerflag,
       studiengang_nr,
       fach_nr,
       unikey,
       anfdat,
       ruebeudat,
       endedat,
       endedat_tsg,
       stufrm,
       kz_rueck_beur_ein,
       kz_rueck_beur_ein_tsg,
       hzb_land_nr,
       hzb_land_sub_r_nr,
       hzb_art_nr,
       hzb_jahr,
       hzb_datum,
       hzb_note,
       abm_art_s,
       abm_art_s_msg,
       po_stp_stp_version_kb,
       po_regelstudienzeit,
       studiengangs_identifikator_msg,
       summe
       )
       
select st_studium_nr,
st_studium_nr_msg,
       st_studstamm_nr,
       matrikelnummer,
       st_studiengang_nr,
       st_studiengang_nr_msg,
       st_studienstatus_nr,
       studienstatustyp,
       statustyp_tsg,
       tsg_generated,
      hrst,
       semester,
       fachsemester,
       praxissemester,
       klinsem,
       hssem,
       primaerflag,
--Nun ist studiengang_nr=1 definiert. Die restlichen werden mit RANK ermittelt:
         (case when studiengang_nr =1 then studiengang_nr
        else 
        dense_rank () over (partition by semester, st_studstamm_nr,studiengang_nr  order by 
         (case when studiengangs_typ_kb='TSG' then studiengangs_identifikator_msg  else st_studium_nr::varchar(255) end ))+1
         end)
  as studiengang_nr,
        (case when primaerflag =1 then 1
       else 
       dense_rank () over (partition by semester, st_studstamm_nr,studiengang_nr  
       order by primaerflag desc,fach_nr)
        end) as fach_nr,
	unikey,
       beginn_datum as anfdat,
      (case when studienstatustyp not in ('Z','X') then  gueltig_ab 
       else null::date  end) as ruebeudat,
       (case when  endedat is not null then endedat 
       when studienstatustyp in ('Z','X') then gueltig_ab 
       else null::date end) as endedat,
       (case when  endedat_tsg is not null then endedat_tsg 
       when statustyp_tsg in ('Z','X') then gueltig_ab 
       else null::date end) as endedat_tsg,
       studienform_statistik_code as stufrm,
       kz_rueck_beur_ein,
       kz_rueck_beur_ein_tsg,
       hzb_land_nr,
       hzb_land_sub_r_nr,
       hzb_art_nr,
       hzb_jahr,
       hzb_datum,
       hzb_note,
       abm_art_s,
       abm_art_s_msg,
       po_stp_stp_version_kb,
       po_regelstudienzeit,
       studiengangs_identifikator_msg,
       1 as summe
       from tmp_studien3
       where kz_rueck_beur_ein is not null
;

-- The below code appends the data for the records having kz_rueck_beur_ein as NULL, this data could be helpful for other diagonostic purpose; here this is segregated 
-- from above to accomodate ranking in  studiengangNummer field
insert into costage_st_studien_aggr(
st_studium_nr,
st_studium_nr_msg,
       st_studstamm_nr,
       matrikelnummer,
       st_studiengang_nr,
       st_studiengang_nr_msg,
       st_studienstatus_nr,
       statustyp,
       statustyp_tsg,
       tsg_generated,
       hrst,
       semester,
       fachsemester,
       praxissemester,
       klinsem,
       hssem,
       primaerflag,
       studiengang_nr,
       fach_nr,
       unikey,
       anfdat,
       ruebeudat,
       endedat,
       endedat_tsg,
       stufrm,
       kz_rueck_beur_ein,
       kz_rueck_beur_ein_tsg,
       hzb_land_nr,
       hzb_land_sub_r_nr,
       hzb_art_nr,
       hzb_jahr,
       hzb_datum,
       hzb_note,
       abm_art_s,
       abm_art_s_msg,
       po_stp_stp_version_kb,
       po_regelstudienzeit,
       studiengangs_identifikator_msg,
       summe
       )
       
select st_studium_nr,
st_studium_nr_msg,
       st_studstamm_nr,
       matrikelnummer,
       st_studiengang_nr,
       st_studiengang_nr_msg,
       st_studienstatus_nr,
       studienstatustyp,
       statustyp_tsg,
       tsg_generated,
       hrst,
       semester,
       fachsemester,
       praxissemester,
       klinsem,
       hssem,
       primaerflag,
--Nun ist studiengang_nr=1 definiert. Die restlichen werden mit RANK ermittelt:
        -- (case when studiengang_nr is not null then studiengang_nr
       -- else 
       -- dense_rank () over (partition by semester, st_studstamm_nr,studiengang_nr  order by 
        -- (case when studiengangs_typ_kb='TSG' then st_absz_nr else st_studium_nr end ))+1
        -- end)
 -- as studiengang_nr,
cast (NULL as int)   as studiengang_nr2,
        (case when primaerflag =1 then 1
       else 
       dense_rank () over (partition by semester, st_studstamm_nr,studiengang_nr  
       order by primaerflag desc, fach_nr)
        end) as fach_nr,
       unikey,
       beginn_datum as anfdat,
      (case when studienstatustyp not in ('Z','X') then gueltig_ab 
       else null::date  end) as ruebeudat,
       (case when  endedat is not null then endedat 
       when studienstatustyp in ('Z','X') then gueltig_ab 
       else null::date end) as endedat,
       (case when  endedat_tsg is not null then endedat_tsg 
       when statustyp_tsg in ('Z','X') then gueltig_ab 
       else null::date end) as endedat_tsg,
       studienform_statistik_code as stufrm,
       kz_rueck_beur_ein,
       kz_rueck_beur_ein_tsg,
       hzb_land_nr,
       hzb_land_sub_r_nr,
       hzb_art_nr,
       hzb_jahr,
       hzb_datum,
       hzb_note,
       abm_art_s,
       abm_art_s_msg,
       po_stp_stp_version_kb,
       po_regelstudienzeit,
       studiengangs_identifikator_msg,
       1 as summe
       from tmp_studien3
      where  kz_rueck_beur_ein is  null
       ;
select 180,timestamp_str(now()) from xdummy;

--Wenn pro Semester und Matrikelnr. keine studiengang_nr=1 existiert, werden diese um 1 reduziert:
select st_studstamm_nr,semester,min(studiengang_nr) as studiengang_nr_min
into temp tmp_studien_ohne_studiengangnr_1
from costage_st_studien_aggr
       where kz_rueck_beur_ein is not null
         group by 1,2
       ;

update costage_st_studien_aggr set studiengang_nr=studiengang_nr -1
where studiengang_nr > 1
and tsg_generated=1
and not exists (select S.st_studstamm_nr from tmp_studien_ohne_studiengangnr_1 S
where S.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr 
and S.semester=costage_st_studien_aggr.semester
and S.studiengang_nr_min=1
)
;

drop table tmp_studien_ohne_studiengangnr_1;       

select 190,timestamp_str(now()) from xdummy;
       
--HZB Data:


--Abmeldungsart:
--zuerst personbezogen:
update costage_st_studien_aggr set abm_art_p=P.st_abm_art_kurzbezeichnung
from costage_st_personen_abmeldungen P, costage_st_semester S
where S.st_sem_nr=P.st_sem_nr
and S.semester_tid=costage_st_studien_aggr.semester
and P.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and P.person_flag='J'
and costage_st_studien_aggr.semester >= ${beginn_semester}
;

select 200,timestamp_str(now()) from xdummy;

--Beurlaubungs-Art:

update costage_st_studien_aggr set urlaub_art=U.st_urlaub_typ_nr
from costage_st_urlaube U, costage_st_semester S
where S.st_sem_nr=U.st_sem_nr
and S.semester_tid=costage_st_studien_aggr.semester
and U.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and costage_st_studien_aggr.semester >= ${beginn_semester}
;

--Wohnorte:
--zuerst Semesterwohnsitz Ort:
update costage_st_studien_aggr set adresse_ort_s=A.land_kfz_kennzeichen_subregion
from costage_st_adressen A
where A.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and A.st_adresse_typ='S'
and costage_st_studien_aggr.semester >= ${beginn_semester}
;
--dann Semesterwohnsitz Staat:
update costage_st_studien_aggr set adresse_staat_s=A.st_land_nr
from costage_st_adressen A
where A.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and A.st_adresse_typ='H'
and costage_st_studien_aggr.semester >= ${beginn_semester}
;
--dann Heimatwohnsitz Ort:
update costage_st_studien_aggr set adresse_ort_h=A.land_kfz_kennzeichen_subregion
from costage_st_adressen A
where A.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and A.st_adresse_typ='H'
and costage_st_studien_aggr.semester >= ${beginn_semester}
;
--dann Heimatwohnsitz Staat:
update costage_st_studien_aggr set adresse_staat_h=A.st_land_nr
from costage_st_adressen A
where A.st_studstamm_nr=costage_st_studien_aggr.st_studstamm_nr
and A.st_adresse_typ='H'
and costage_st_studien_aggr.semester >= ${beginn_semester}
;

select 300,timestamp_str(now()) from xdummy;


drop table tmp_studien3;
drop table tmp_primaer_msg;


