src: |
  SELECT p_sejours.finess,
      p_sejours.no_sejour,
      p_sejours.oid AS sejour_id,
      p_sejours.no_patient,
      p_sejours.code_sexe,
          CASE
              WHEN p_sejours.code_sexe = '1'::bpchar THEN 'M'::bpchar
              WHEN p_sejours.code_sexe = '2'::bpchar THEN 'F'::bpchar
              ELSE 'M'::bpchar
          END AS sexe_texte_court,
      p_sejours.age,
      p_sejours.code_prevu,
      p_sejours.type_sejour,
          CASE
              WHEN p_sejours.type_sejour = '1'::text THEN 'Hospitalisés'::text
              WHEN p_sejours.type_sejour = '2'::text THEN 'Ambulatoires'::text
              WHEN p_sejours.type_sejour = '3'::text THEN 'Externes'::text
              WHEN p_sejours.type_sejour = '4'::text THEN 'Long séjours'::text
              WHEN p_sejours.type_sejour = '5'::text THEN 'Séances'::text
              WHEN p_sejours.type_sejour = '6'::text THEN 'Bébés sans séjour'::text
              WHEN p_sejours.type_sejour = '9'::text THEN 'Fictifs'::text
              ELSE p_sejours.type_sejour
          END AS type_sejour_texte,
      t_lieux_c.mode_traitement_id,
      t_lieux_c.mode_traitement_code,
      t_lieux_c.mode_traitement_texte,
      to_char(p_sejours.date_entree::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_entree,
      p_sejours.date_entree,
      p_sejours.heure_entree,
      p_sejours.code_sorti,
      to_char(p_sejours.date_sortie::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_sortie,
      p_sejours.date_sortie,
      p_sejours.heure_sortie,
      p_sejours.ghs_id,
      t_ghs_c.ghs_code_text AS ghs_code,
      t_ghs_c.ghs_texte,
      p_sejours.ghm_id,
      t_ghm_c.ghm_code,
      t_ghm_c.ghm_texte,
      t_ghm_c.ghm5_id,
      t_ghm_c.ghm5_code,
      t_ghm_c.ghm5_texte,
      t_ghm_c.severite_ghm_id,
      t_ghm_c.severite_ghm_code,
      t_ghm_c.severite_ghm_texte,
      t_ghm_c.cas_id,
      t_ghm_c.cas_code,
      t_ghm_c.cas_texte,
      t_ghm_c.mco_id,
      t_ghm_c.mco_code,
      t_ghm_c.mco_texte,
      t_ghm_c.cmd_id,
      t_ghm_c.cmd_code,
      t_ghm_c.cmd_texte,
      t_ghm_c.mco_id AS mcos_id,
      t_ghm_c.mco_code AS mcos_code,
      t_ghm_c.mco_texte AS mcos_texte,
      t_ghm_c.ghm_section_id,
      t_ghm_c.ghm_section_code,
      t_ghm_c.ghm_section_texte,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN 0
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN 1
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN 2
              ELSE 0
          END AS type_tiers_payant_id,
      t_types_tiers_payant.code AS type_tiers_payant_code,
      t_types_tiers_payant.texte_court AS type_tiers_payant_texte,
      p_sejours.medecin_sejour_id AS medecin_adm_sejour_id,
      t_medecins_administratifs_c.adm_code AS medecin_adm_sejour_code,
      t_medecins_administratifs_c.adm_nom AS medecin_adm_sejour_nom,
      t_medecins_administratifs_c.adm_prenom AS medecin_adm_sejour_prenom,
      t_medecins_administratifs_c.adm_nom_prenom AS medecin_adm_sejour_nom_prenom,
      t_medecins_administratifs_c.section_id AS medecin_adm_sejour_section_id,
      t_medecins_administratifs_c.section_code AS medecin_adm_sejour_section_code,
      t_medecins_administratifs_c.section_texte AS medecin_adm_sejour_section_texte,
      t_medecins_administratifs_c.medecin_id AS medecin_sejour_id,
      t_medecins_administratifs_c.nom AS medecin_sejour_nom,
      t_medecins_administratifs_c.prenom AS medecin_sejour_prenom,
      t_medecins_administratifs_c.nom_prenom AS medecin_sejour_nom_prenom,
      t_medecins_administratifs_c.specialite_id AS medecin_sejour_specialite_id,
      t_medecins_administratifs_c.specialite_code AS medecin_sejour_specialite_code,
      t_medecins_administratifs_c.specialite_texte AS medecin_sejour_specialite_texte,
      p_sejours.lieu_sortie_id,
      t_lieux_c.service_facturation_id AS lieu_sortie_service_id,
      t_lieux_c.service_facturation_code AS lieu_sortie_service_code,
      t_lieux_c.service_facturation_texte AS lieu_sortie_service_texte,
      t_lieux_c.service_facturation_section_id AS lieu_sortie_service_section_id,
      t_lieux_c.service_facturation_section_code AS lieu_sortie_service_section_code,
      t_lieux_c.service_facturation_section_texte AS lieu_sortie_service_section_texte,
      t_lieux_c.service_facturation_section_nb_lits AS lieu_sortie_service_section_nb_lits,
      t_lieux_c.service_facturation_section_nb_cp AS lieu_sortie_service_section_nb_cp,
      t_lieux_c.activite_id AS lieu_sortie_activite_id,
      t_lieux_c.activite_code AS lieu_sortie_activite_code,
      t_lieux_c.activite_texte AS lieu_sortie_activite_texte,
      t_lieux_c.etage_id AS lieu_sortie_etage_id,
      t_lieux_c.etage_code AS lieu_sortie_etage_code,
      t_lieux_c.etage_texte AS lieu_sortie_etage_texte,
      t_lieux_c.etage_section_id AS lieu_sortie_etage_section_id,
      t_lieux_c.etage_section_code AS lieu_sortie_etage_section_code,
      t_lieux_c.etage_section_texte AS lieu_sortie_etage_section_texte,
      t_lieux_c.lit_id AS lieu_sortie_lit_id,
      t_lieux_c.lit_code AS lieu_sortie_lit_code,
      t_lieux_c.lit_texte AS lieu_sortie_lit_texte,
      t_lieux_c.lit_section_id AS lieu_sortie_lit_section_id,
      t_lieux_c.lit_section_code AS lieu_sortie_lit_section_code,
      t_lieux_c.lit_section_texte AS lieu_sortie_lit_section_texte,
      t_lieux_c.unite_medicale_id AS lieu_sortie_unite_medicale_id,
      t_lieux_c.unite_medicale_code AS lieu_sortie_unite_medicale_code,
      t_lieux_c.unite_medicale_texte AS lieu_sortie_unite_medicale_texte,
      t_lieux_c.unite_medicale_section_id AS lieu_sortie_unite_medicale_section_id,
      t_lieux_c.unite_medicale_section_code AS lieu_sortie_unite_medicale_section_code,
      t_lieux_c.unite_medicale_section_texte AS lieu_sortie_unite_medicale_section_texte,
      t_lieux_c.unite_fonctionnelle_id AS lieu_sortie_unite_fonctionnelle_id,
      t_lieux_c.unite_fonctionnelle_code AS lieu_sortie_unite_fonctionnelle_code,
      t_lieux_c.unite_fonctionnelle_texte AS lieu_sortie_unite_fonctionnelle_texte,
      t_lieux_c.unite_fonctionnelle_section_id AS lieu_sortie_unite_fonctionnelle_section_id,
      t_lieux_c.unite_fonctionnelle_section_code AS lieu_sortie_unite_fonctionnelle_section_code,
      t_lieux_c.unite_fonctionnelle_section_texte AS lieu_sortie_unite_fonctionnelle_section_texte,
      to_char(
          CASE
              WHEN p_sejours.ghs_id > 0 THEN p_sejours.date_groupage
              ELSE '2099-12-31'::date
          END::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_groupage,
          CASE
              WHEN p_sejours.ghs_id > 0 THEN p_sejours.date_groupage
              ELSE '2099-12-31'::date
          END AS date_groupage,
          CASE
              WHEN t_lieux_c.type_t2a = '1'::bpchar AND (p_sejours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) AND p_sejours.ghs_id > 0 AND p_sejours.date_groupage < '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_delai_groupage,
          CASE
              WHEN t_lieux_c.type_t2a = '1'::bpchar AND (p_sejours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) AND p_sejours.ghs_id > 0 AND p_sejours.date_groupage < '2099-12-31'::date THEN p_sejours.delai_groupage
              ELSE 0::numeric
          END AS delai_groupage,
          CASE
              WHEN p_sejours.type_sejour <> ALL (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text]) THEN 0
              WHEN t_lieux_c.type_t2a <> '1'::bpchar OR p_sejours.date_groupage < '2099-12-31'::date AND p_sejours.ghs_id > 0 THEN 0
              ELSE 1
          END AS nb_non_groupe,
      to_char(p_sejours.date_facture::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_facture,
      p_sejours.date_facture,
          CASE
              WHEN p_sejours.date_facture < '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_delai_facture,
          CASE
              WHEN p_sejours.date_facture < '2099-12-31'::date THEN p_sejours.delai_facture
              ELSE 0::numeric
          END AS delai_facture,
          CASE
              WHEN p_sejours.date_facture = '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_non_facture,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN to_char(p_sejours.date_expedition_0::timestamp with time zone, 'YYYYMM'::text)::numeric
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN to_char(p_sejours.date_expedition_1::timestamp with time zone, 'YYYYMM'::text)::numeric
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN to_char(p_sejours.date_expedition_2::timestamp with time zone, 'YYYYMM'::text)::numeric
              ELSE NULL::numeric
          END AS mois_expedition,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.date_expedition_0
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.date_expedition_1
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.date_expedition_2
              ELSE NULL::date
          END AS date_expedition,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar AND p_sejours.date_expedition_0 < '2099-12-31'::date THEN 1
              WHEN t_types_tiers_payant.code = '1'::bpchar AND p_sejours.date_expedition_1 < '2099-12-31'::date THEN 1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND p_sejours.date_expedition_2 < '2099-12-31'::date THEN 1
              ELSE 0
          END AS nb_delai_expedition,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar AND p_sejours.date_expedition_0 < '2099-12-31'::date THEN p_sejours.delai_expedition_0
              WHEN t_types_tiers_payant.code = '1'::bpchar AND p_sejours.date_expedition_1 < '2099-12-31'::date THEN p_sejours.delai_expedition_1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND p_sejours.date_expedition_2 < '2099-12-31'::date THEN p_sejours.delai_expedition_2
              ELSE 0::numeric
          END AS delai_expedition,
          CASE
              WHEN t_types_tiers_payant.code = '1'::bpchar AND (p_sejours.date_expedition_1 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_1_c <> p_sejours.montant_regle_1_c OR p_sejours.montant_comptabilise_1_h <> p_sejours.montant_regle_1_h) OR p_sejours.date_facture = '2099-12-31'::date) THEN 1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND (p_sejours.date_expedition_2 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_2_c <> p_sejours.montant_regle_2_c OR p_sejours.montant_comptabilise_2_h <> p_sejours.montant_regle_2_h) OR p_sejours.date_facture = '2099-12-31'::date) THEN 1
              ELSE 0
          END AS nb_non_expedie,
          CASE
              WHEN t_types_tiers_payant.code = '1'::bpchar AND p_sejours.date_expedition_1 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_1_c <> p_sejours.montant_regle_1_c OR p_sejours.montant_comptabilise_1_h <> p_sejours.montant_regle_1_h) THEN p_sejours.montant_comptabilise_1_c + p_sejours.montant_comptabilise_1_h - p_sejours.montant_regle_1_c - p_sejours.montant_regle_1_h
              WHEN t_types_tiers_payant.code = '2'::bpchar AND p_sejours.date_expedition_2 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_2_c <> p_sejours.montant_regle_2_c OR p_sejours.montant_comptabilise_2_h <> p_sejours.montant_regle_2_h) THEN p_sejours.montant_comptabilise_2_c + p_sejours.montant_comptabilise_2_h - p_sejours.montant_regle_2_c - p_sejours.montant_regle_2_h
              ELSE 0::numeric
          END AS montant_non_expedie,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN to_char(p_sejours.date_solde_0::timestamp with time zone, 'YYYYMM'::text)::numeric
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN to_char(p_sejours.date_solde_1::timestamp with time zone, 'YYYYMM'::text)::numeric
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN to_char(p_sejours.date_solde_2::timestamp with time zone, 'YYYYMM'::text)::numeric
              ELSE NULL::numeric
          END AS mois_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.date_solde_0
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.date_solde_1
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.date_solde_2
              ELSE NULL::date
          END AS date_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_0 < '2099-12-31'::date AND p_sejours.montant_regle_0_c = p_sejours.montant_comptabilise_0_c AND p_sejours.montant_regle_0_h = p_sejours.montant_comptabilise_0_h THEN 1
              WHEN t_types_tiers_payant.code = '1'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_1 < '2099-12-31'::date AND p_sejours.montant_regle_1_c = p_sejours.montant_comptabilise_1_c AND p_sejours.montant_regle_1_h = p_sejours.montant_comptabilise_1_h THEN 1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_2 < '2099-12-31'::date AND p_sejours.montant_regle_2_c = p_sejours.montant_comptabilise_2_c AND p_sejours.montant_regle_2_h = p_sejours.montant_comptabilise_2_h THEN 1
              ELSE 0
          END AS nb_delai_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_0 < '2099-12-31'::date AND p_sejours.montant_regle_0_c = p_sejours.montant_comptabilise_0_c AND p_sejours.montant_regle_0_h = p_sejours.montant_comptabilise_0_h THEN p_sejours.delai_solde_0
              WHEN t_types_tiers_payant.code = '1'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_1 < '2099-12-31'::date AND p_sejours.montant_regle_1_c = p_sejours.montant_comptabilise_1_c AND p_sejours.montant_regle_1_h = p_sejours.montant_comptabilise_1_h THEN p_sejours.delai_solde_1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde_2 < '2099-12-31'::date AND p_sejours.montant_regle_2_c = p_sejours.montant_comptabilise_2_c AND p_sejours.montant_regle_2_h = p_sejours.montant_comptabilise_2_h THEN p_sejours.delai_solde_2
              ELSE 0::numeric
          END AS delai_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar AND (p_sejours.date_facture = '2099-12-31'::date OR p_sejours.montant_regle_0_c <> p_sejours.montant_comptabilise_0_c OR p_sejours.montant_regle_0_h <> p_sejours.montant_comptabilise_0_h) THEN 1
              WHEN t_types_tiers_payant.code = '1'::bpchar AND (p_sejours.date_facture = '2099-12-31'::date OR p_sejours.montant_regle_1_c <> p_sejours.montant_comptabilise_1_c OR p_sejours.montant_regle_1_h <> p_sejours.montant_comptabilise_1_h) THEN 1
              WHEN t_types_tiers_payant.code = '2'::bpchar AND (p_sejours.date_facture = '2099-12-31'::date OR p_sejours.montant_regle_2_c <> p_sejours.montant_comptabilise_2_c OR p_sejours.montant_regle_2_h <> p_sejours.montant_comptabilise_2_h) THEN 1
              ELSE 0
          END AS nb_non_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_comptabilise_0_c + p_sejours.montant_comptabilise_0_h - p_sejours.montant_regle_0_c - p_sejours.montant_regle_0_h
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_comptabilise_1_c + p_sejours.montant_comptabilise_1_h - p_sejours.montant_regle_1_c - p_sejours.montant_regle_1_h
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_comptabilise_2_c + p_sejours.montant_comptabilise_2_h - p_sejours.montant_regle_2_c - p_sejours.montant_regle_2_h + p_sejours.montant_comptabilise_22_c + p_sejours.montant_comptabilise_22_h - p_sejours.montant_regle_22_c - p_sejours.montant_regle_22_h
              ELSE 0::numeric
          END AS montant_non_solde,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_facture_0_c
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_facture_1_c
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_facture_2_c + p_sejours.montant_facture_22_c
              ELSE 0::numeric
          END AS montant_facture_c,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_facture_0_h
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_facture_1_h
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_facture_2_h + p_sejours.montant_facture_22_h
              ELSE 0::numeric
          END AS montant_facture_h,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_comptabilise_0_c
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_comptabilise_1_c
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_comptabilise_2_c + p_sejours.montant_comptabilise_22_c
              ELSE 0::numeric
          END AS montant_comptabilise_c,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_comptabilise_0_h
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_comptabilise_1_h
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_comptabilise_2_h + p_sejours.montant_comptabilise_22_h
              ELSE 0::numeric
          END AS montant_comptabilise_h,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_regle_0_c
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_regle_1_c
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_regle_2_c + p_sejours.montant_regle_22_c
              ELSE 0::numeric
          END AS montant_regle_c,
          CASE
              WHEN t_types_tiers_payant.code = '0'::bpchar THEN p_sejours.montant_regle_0_h
              WHEN t_types_tiers_payant.code = '1'::bpchar THEN p_sejours.montant_regle_1_h
              WHEN t_types_tiers_payant.code = '2'::bpchar THEN p_sejours.montant_regle_2_h + p_sejours.montant_regle_22_h
              ELSE 0::numeric
          END AS montant_regle_h,
      1 AS nb_sejours,
      p_sejours.nb_factures,
      p_sejours.nb_factures_regularisation,
      p_sejours.nb_factures_rejet,
      0 AS no_oid,
          CASE
              WHEN p_sejours.type_sejour = ANY (ARRAY['3'::bpchar::text, '5'::bpchar::text]) THEN 0
              WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie IS NOT NULL THEN p_sejours.date_sortie - p_sejours.date_entree
              WHEN p_sejours.type_sejour = '1'::text AND date(now()) > p_sejours.date_entree THEN date(now()) - p_sejours.date_entree
              ELSE 0
          END AS duree,
      p_sejours.risque_id,
          CASE
              WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN '1'::text
              WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN '2'::text
              WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN '3'::text
              WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN '4'::text
              WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN '5'::text
              WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN '6'::text
              WHEN
              CASE
                  WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                  ELSE 0
              END::numeric > 2::numeric THEN '7'::text
              WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN '8'::text
              ELSE NULL::text
          END AS severite_cti_code,
          CASE
              WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN 'Séances'::text
              WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN 'Sévérité J'::text
              WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN 'Sévérité T'::text
              WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN 'Sévérité Z'::text
              WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN 'Sévérité 1'::text
              WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN 'Sévérité ABCDE'::text
              WHEN
              CASE
                  WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                  ELSE 0
              END::numeric > 2::numeric THEN 'DS > 2 nuits'::text
              WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN 'Non PMSI'::text
              ELSE NULL::text
          END AS severite_cti_texte,
          CASE
              WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN '1'::text
              WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN '2'::text
              WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN '3'::text
              WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN '4'::text
              WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN '5'::text
              WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN '6'::text
              WHEN
              CASE
                  WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                  ELSE 0
              END::numeric > 2::numeric THEN '7'::text
              WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN '8'::text
              ELSE NULL::text
          END AS severite_cti_id
     FROM activite.p_sejours
       JOIN activite.t_types_tiers_payant ON t_types_tiers_payant.code = '0'::bpchar AND p_sejours.tiers_payant_0_id > 0 OR t_types_tiers_payant.code = '1'::bpchar AND p_sejours.tiers_payant_1_id > 0 OR t_types_tiers_payant.code = '2'::bpchar AND (p_sejours.tiers_payant_2_id > 0 OR p_sejours.tiers_payant_22_id > 0)
       JOIN activite.t_medecins_administratifs_c ON p_sejours.medecin_sejour_id = t_medecins_administratifs_c.oid
       JOIN activite.t_lieux_c ON p_sejours.lieu_sortie_id = t_lieux_c.oid
       JOIN activite.t_ghs_c ON p_sejours.ghs_id = t_ghs_c.ghs_id
       JOIN activite.t_ghm_c ON p_sejours.ghm_id = t_ghm_c.ghm_id
    WHERE p_sejours.etat = ''::bpchar;