meine Statistik ist die Orginal tomedo.
drop table if exists tempOPPlanTable;
create temporary table tempOPPlanTable as
select
K.ident as op_ident,
T.beginn as Zeit,
CASE
WHEN date_part('dow', T.beginn) = 1 THEN 'Montag'
WHEN date_part('dow', T.beginn) = 2 THEN 'Dienstag'
WHEN date_part('dow', T.beginn) = 3 THEN 'Mittwoch'
WHEN date_part('dow', T.beginn) = 4 THEN 'Donnerstag'
WHEN date_part('dow', T.beginn) = 5 THEN 'Freitag'
WHEN date_part('dow', T.beginn) = 6 THEN 'Samstag'
WHEN date_part('dow', T.beginn) = 0 THEN 'Sonntag'
ELSE 'Unbekannt'
END as Wochentag,
(to_timestamp(extract (epoch from T.beginn) + T.laenge * 60) AT TIME ZONE 'UTC') as ende,
T.laenge as Termindauer,
TA.kuerzel as Terminkuerzel,
TA.bezeichnung as Terminbezeichnung,
KT.kuerzel as karteiTyp
from Karteieintrag K
left join Termin T on (T.ident = K.termin_ident)
left join Karteieintragtyp KT on (KT.ident = K.karteieintragtyp_ident)
left join KarteiEintragMediaTyp KMT on (KMT.ident = K.mediaTyp_ident and KMT.name = 'OP')
left join Terminart TA on (T.terminart_ident = TA.ident)
where
( KT.kuerzel = 'OPAnm') and
K.visible = true
;
-- zusammensammeln
select
0 as nummer,
KS.op_ident,
-- KS.op_ident as OPIDENT,
KS.zeit,
KS.wochentag,
KS.Termindauer,
KS.karteityp,
KS.Terminkuerzel,
KS.Terminbezeichnung,
P.ident as patientID,
P.vorname,
P.nachname,
P.titel,
cast(P.geburtsDatum as date),
PD.geschlecht,
PKD.telefon,
PKD.handynummer,
ADD.strasse,
ADD.plz,
P.ort as wohnort,
UE.nachname as ueberweiser,
WB.nachname as weiterbehandler,
HA.nachname as hausarzt,
OP1.kuerzel as Operateur1,
OP2.kuerzel as Operateur2,
ANA.kuerzel as Anestesist,
CASE
WHEN PD.privatPatient THEN 'Privat'
ELSE 'GKV'
END as Versichertenstatus,
-- KD.kassenname,
CASE
WHEN pk.ident is not null THEN pk.kurzname
ELSE kd.abrechnenderKostentraegerName
END as kassenname,
T.info as TerminInfo,
T.removed as TerminGeloescht,
T.warda as TerminWarDa,
K.opFreitext as Bemerkung,
CKEE.name as bezeichner,
CASE WHEN CKEE.modus = 'BMI' THEN
ARRAY_TO_STRING(ARRAY['<zsBMI_start>',CKEE.value, '<zsBMI_seperator>', CKEE.auswahl, '<zsBMI_stop>'], '')
ELSE value END as wert,
CKEE.modus as zsModus
from tempOPPlanTable KS
join Karteieintrag K on (K.ident = KS.op_ident)
left join karteieintrag_customKarteiEintragEntries CKEES on (CKEES.karteieintrag_ident = KS.op_ident)
left join CustomKarteiEintragEntry CKEE on (CKEE.ident = CKEES.customKarteiEintragEntries_ident and CKEE.removed = false)
join Patientendetailsrelationen_karteieintraege PDR on (PDR.karteieintraege_ident = KS.op_ident)
join Patientendetails PD on (PD.patientendetailsrelationen_ident = PDR.patientendetailsrelationen_ident)
join Patient P on (P.patientendetails_ident = PD.ident)
left join kontaktdaten PKD on (PD.kontaktdaten_ident = PKD.ident)
left join adresse ADD on (PKD.adresse_ident = ADD.ident)
left join kartendaten KD on (PD.aktuellegvkkartendaten_ident = KD.ident)
left join kartendaten KDp on (PD.aktuellepvkartendaten_ident = KDp.ident)
left join Krankenkasse PK on (KDP.privatkasse_ident = PK.ident)
left join hausarzt HA on (PD.hausarzt_ident = HA.ident)
left join hausarzt UE on (K.ueberweiser_ident = UE.ident)
left join hausarzt WB on (K.folgearzt_ident = WB.ident)
left join Nutzer OP1 on (OP1.ident = K.operateur1_ident)
left join Nutzer OP2 on (OP2.ident = K.operateur2_ident)
left join Nutzer ANA on (ANA.ident = K.anestesist_ident)
left join Termin T on (T.ident = K.termin_ident)
order by KS.zeit, KS.op_ident
;