EXPLAIN ANALYZE
SELECT 
J1033386.name  AS  sectie_internare , 
J1033387.name  AS  sectie_externare , 
CASE WHEN(J1037911.id > 0)  THEN(1)  ELSE(0) END  AS  ic_principala , 
TO_CHAR(J1031101.validfrom , 'YYYY-MM-DD HH24:MI')  AS  validfrom , 
TO_CHAR(J1033359.validto , 'YYYY-MM-DD HH24:MI')  AS  validto , 
J1037910.urbanflag ,
J1031076.name  AS  sex , 
J1031075.cnp  AS  popup_cnp_pacient , 
J1031075.name  AS  text_nume_pacient , 
J1031075.birthdate  AS  data_nasterii , 
J1031075.surname  AS  text_prenume_pacient , 
J1031078.value  AS  popup_tip_asigurare , 
J1031079.value  AS  popup_statut_asigurat , 
J1031080.code  AS  popup_cas_asigurat , 
J1031081.code  AS  popup_categorie_asigurare , 
J1031082.name  AS  popup_tip_internare , 
J1031083.name  AS  popup_criteriu_internare , 
J1031084.stencil_no  AS  popup_medic_curant , 
J1031084.id  AS  _popup_medic_curant , 
J1031089.value  AS  popup_nivel_instruire , 
J1031090.nume  AS  popup_situatii_speciale , 
J1031091.nume  AS  popup_internat_prin , 
J1031091.id  AS  _popup_internat_prin , 
J1031092.nume  AS  popup_formulare_europene , 
J1031094.cnp  AS  popup_cnp_mama , 
J1031093.nrfo  AS  popup_fo_mama , 
J1031088.id  AS  _popup_ocupatia , 
J1031088.name  AS  popup_ocupatia , 
J1031095.id  AS  _popup_sursa_internare , 
J1031095.denumire  AS  popup_sursa_internare , 
J1031096.id  AS  _popup_diseasecategory , 
J1031096.code  AS  popup_diseasecategory , 
J1031097.id  AS  _popup_diagnostic_internare_icd10 , 
J1031097.name  AS  popup_diagnostic_internare_icd10 , 
J1031098.id  AS  _popup_mod_contract , 
J1031098.description  AS  popup_mod_contract , 
J1031099.id  AS  _popup_criteriu_urgenta , 
J1031099.name  AS  popup_criteriu_urgenta , 
J1031100.id  AS  _popup_exceptie_bi , 
J1031100.description  AS  popup_exceptie_bi , 
J1031087.id  AS  _popup_intocmit , 
J1031087.stencil_no  AS  popup_intocmit , 
J1031736.id  AS  _popup_tip_externare , 
J1031736.descriere  AS  popup_tip_externare , 
J1031737.id  AS  _popup_stare_externare , 
J1031737.descriere  AS  popup_stare_externare , 
J1033295.id  AS  _popup_chirurg , 
J1033295.stencil_no  AS  popup_chirurg , 
J1033299.id  AS  _text_caz_externare , 
J1033299.description  AS  text_caz_externare , 
J1033304.id  AS  _popup_cetatenie , 
J1033304.description  AS  popup_cetatenie , 
J1033358.id  AS  _popup_tip_cetatenie , 
J1033358.name  AS  popup_tip_cetatenie , 
J1034618.id  AS  _popup_fisa_upu , 
J1034618.nr_fupu  AS  popup_fisa_upu , 
J1034962.id  AS  _id_country , 
J1034962.code  AS  id_country , 
J1034965.id  AS  _id_persontype , 
J1034965.code  AS  id_persontype 
FROM focg AS J1031074 
LEFT JOIN focg_procedures AS J1037911 ON ( J1037911.id_focg = J1031074.id AND  J1037911.este_ic_principala = 1) 
LEFT JOIN focg_procedures AS J1037912 ON ( J1037912.id_focg = J1031074.id AND  J1037912.este_ic_principala = 1 AND  J1037912.id < J1037911.id) 
LEFT JOIN address AS J1037908 ON ( J1037908.id = J1031074.id_address_domiciliu) 
LEFT JOIN city AS J1037909 ON ( J1037909.id = J1037908.idcity) 
LEFT JOIN citytype AS J1037910 ON ( J1037910.id = J1037909.citytype) 
JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg = J1031074.id) 
JOIN department AS J1033386 ON ( J1033386.id = J1031101.iddepartment) 
LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg = J1031074.id AND  J1031102.validfrom < J1031101.validfrom) 
JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg = J1031074.id) 
JOIN department AS J1033387 ON ( J1033387.id = J1033359.iddepartment) 
LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg = J1031074.id AND  J1033360.validfrom > J1033359.validfrom) 
LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient) 
LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex) 
LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id = J1031074.id_focg_serials) 
LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id = J1031074.idensuredstatustype) 
LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id = J1031074.idensuredstatus) 
LEFT JOIN org_units AS J1031080 ON ( J1031080.id = J1031074.id_org_unit) 
LEFT JOIN personstate AS J1031081 ON ( J1031081.id = J1031074.id_categorie_asigurat) 
LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id = J1031074.id_focg_tip_internare) 
LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id = J1031074.id_focg_criteriu_internare) 
LEFT JOIN physicians AS J1031084 ON ( J1031084.id = J1031074.idphysiciancurrent) 
LEFT JOIN personnel AS J1031085 ON ( J1031085.id = J1031084.id_personnel) 
LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson) 
LEFT JOIN physicians AS J1031087 ON ( J1031087.id = J1031074.idphysician_madeby) 
LEFT JOIN proffession AS J1031088 ON ( J1031088.id = J1031074.idproffesion) 
LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id = J1031074.id_education_level) 
LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id = J1031074.id_focg_situatii_speciale) 
LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id = J1031074.id_focg_internat_prin) 
LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id = J1031074.id_formulare_europene) 
LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg) 
LEFT JOIN person AS J1031094 ON ( J1031094.id = J1031074.id_cnp_mama) 
LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id = J1031074.id_focg_sursa_internare) 
LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id = J1031074.id_diseasecategory) 
LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10) 
LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id = J1031074.id_focg_contract_modes) 
LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id = J1031074.id_focg_criterii_urgenta) 
LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id = J1031074.id_exceptie_bilet_internare) 
LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id = J1031074.id_focg_tip_externare) 
LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id = J1031074.id_focg_stare_externare) 
LEFT JOIN physicians AS J1033295 ON ( J1033295.id = J1031074.idphysician_surgeon) 
LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id = J1031074.id_hospital_release_statuses) 
LEFT JOIN citizenship AS J1033304 ON ( J1033304.id = J1031074.id_citizenship) 
LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id = J1031074.id_drg_tip_cetatenie) 
LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id = J1031074.id_stari_spitalizari) 
LEFT JOIN fupu AS J1034618 ON ( J1034618.id = J1031074.id_fupu) 
LEFT JOIN country AS J1034962 ON ( J1034962.id = J1031074.id_country) 
LEFT JOIN persontype AS J1034965 ON ( J1034965.id = J1031074.id_persontype) 
WHERE   J1031102.id IS NULL   AND    J1033360.id IS NULL   AND J1037912.id IS NULL   AND    J1031074.id_stari_spitalizari = 1  
ORDER BY J1031101.validfrom DESC
LIMIT  20 OFFSET 0
