src: |
  SELECT p_rss.finess,
      t_finess.oid AS finess_id,
      t_finess.texte_court AS finess_texte,
      t_finess.section_id AS finess_section_id,
      t_finess.section_code AS finess_section_code,
      t_finess.section_texte AS finess_section_texte,
      p_rss.patient_id,
      p_rss.no_patient,
      p_rss.oid AS rss_id,
      p_rss.no_rss,
      p_rss.no_sejour_administratif,
      p_rss.traitement_epmsi,
      p_rss.code_retour_groupage,
      p_rss.en_cours,
      p_rss.en_cours_etat,
          CASE
              WHEN p_rss.en_cours LIKE '1' AND p_rss.en_cours_etat = 'F'::bpchar THEN 'FA'::text
              WHEN p_rss.en_cours LIKE '1' AND p_rss.en_cours_etat = 'V'::bpchar THEN 'GV'::text
              WHEN p_rss.en_cours LIKE '1' AND p_rss.en_cours_etat = 'G'::bpchar THEN 'GR'::text
              WHEN p_rss.en_cours LIKE '1' AND p_rss.en_cours_etat = 'S'::bpchar THEN 'SO'::text
              WHEN p_rss.en_cours LIKE '1' AND p_rss.en_cours_etat = 'P'::bpchar THEN 'PR'::text
              WHEN p_rss.en_cours LIKE '1' THEN 'NX'::text
              ELSE 'EX'::text
          END AS en_cours_texte_tres_court,
      p_rss.mois_sortie,
      (
          CASE
              WHEN date_part('month'::text, p_rss.date_sortie) = 1::double precision THEN 'Janvier'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 2::double precision THEN 'Février'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 3::double precision THEN 'Mars'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 4::double precision THEN 'Avril'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 5::double precision THEN 'Mai'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 6::double precision THEN 'Juin'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 7::double precision THEN 'Juillet'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 8::double precision THEN 'Aout'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 9::double precision THEN 'Septembre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 10::double precision THEN 'Octobre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 11::double precision THEN 'Novembre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 12::double precision THEN 'Décembre'::text
              ELSE date_part('month'::text, p_rss.date_sortie)::text
          END || ' '::text) || date_part('year'::text, p_rss.date_sortie)::text AS mois_sortie_texte,
      to_char(date_part('month'::text, p_rss.date_sortie), '09'::text) AS mois_sortie_mm_code,
          CASE
              WHEN date_part('month'::text, p_rss.date_sortie) = 1::double precision THEN 'Janvier'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 2::double precision THEN 'Février'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 3::double precision THEN 'Mars'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 4::double precision THEN 'Avril'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 5::double precision THEN 'Mai'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 6::double precision THEN 'Juin'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 7::double precision THEN 'Juillet'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 8::double precision THEN 'Aout'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 9::double precision THEN 'Septembre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 10::double precision THEN 'Octobre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 11::double precision THEN 'Novembre'::text
              WHEN date_part('month'::text, p_rss.date_sortie) = 12::double precision THEN 'Décembre'::text
              ELSE date_part('month'::text, p_rss.date_sortie)::text
          END AS mois_sortie_mm_texte,
      p_rss.date_entree,
      p_rss.date_sortie,
      p_rss.comite_medical_id,
      p_rss.unite_medicale_principale_id,
      p_rss.rehosp_meme_ghm,
      p_rss.mode_entree,
      p_rss.provenance,
      p_rss.mode_sortie,
      p_rss.destination,
      p_rss.sexe,
      t_sexe.texte AS sexe_texte,
      t_sexe.texte_court AS sexe_texte_court,
      p_rss.date_naissance,
      p_rss.age,
      p_rss.code_postal,
      p_rss.code_postal_id,
      p_rss.severite_fg11_simulation_code,
      p_rss.ghm_id,
      t_ghm_c.code AS ghm_code,
      t_ghm_c.texte AS ghm_texte,
      t_ghm_c.section_id AS ghm_section_id,
      t_ghm_c.section_code AS ghm_section_code,
      t_ghm_c.section_texte AS ghm_section_texte,
      t_ghm_c.ghm5_id,
      t_ghm_c.ghm5_code,
      t_ghm_c.ghm5_texte,
      t_ghm_c.ghm_prefixe,
      t_ghm_c.ghm_suffixe,
      t_ghm_c.mco,
      t_ghm_c.mco_texte,
      t_ghm_c.severite_ghm_code,
      t_ghm_c.severite_ghm_texte,
      t_ghm_c.cmd_id,
      t_ghm_c.cmd_code,
      t_ghm_c.cmd_texte,
      t_ghm_c.cas_id,
      t_ghm_c.cas_code,
      t_ghm_c.cas_texte,
      p_rss.nb_rum,
      p_rss.nb_exh,
      p_rss.ca_exh,
      p_rss.nb_exb,
      p_rss.ca_exb,
      p_rss.ca_ghs_theorique - p_rss.ca_ghs AS ca_perte_exb,
      p_rss.nb_ghs,
      p_rss.ca_ghs,
      p_rss.ca_ghs_theorique,
      p_rss.forfait_facture,
      t_ghm_c.cmd_24,
      t_ghm_c.cmd_28,
      t_ghm_c.cmd_n24n28,
      t_ghm_c.pole_oap_id,
      t_ghm_c.pole_oap_code,
      t_ghm_c.pole_oap_texte,
      t_ghm_c.ligne_oap_id,
      t_ghm_c.ligne_oap_code,
      t_ghm_c.ligne_oap_texte,
      t_medecins_c.specialite_id AS specialite_medecin_rss_id,
      t_medecins_c.specialite_medecin_code AS specialite_medecin_rss_code,
      t_medecins_c.specialite_medecin_texte AS specialite_medecin_rss_texte,
      t_medecins_c.specialite_interne_id AS specialite_interne_medecin_rss_id,
      t_medecins_c.specialite_interne_medecin_code AS specialite_interne_medecin_rss_code,
      t_medecins_c.specialite_interne_medecin_texte AS specialite_interne_medecin_rss_texte,
      p_rss.medecin_rss_id,
      t_medecins_c.code AS medecin_rss_code,
      t_medecins_c.nom_prenom AS medecin_rss_nom,
      t_medecins_c.specialite_medecin_reference_id AS specialite_medecin_reference_rss_id,
      t_medecins_c.specialite_medecin_reference_code AS specialite_medecin_reference_rss_code,
      t_medecins_c.specialite_medecin_reference_texte AS specialite_medecin_reference_rss_texte,
      t_medecins_c.specialite_interne_medecin_reference_id AS specialite_interne_medecin_reference_rss_id,
      t_medecins_c.specialite_interne_medecin_reference_code AS specialite_interne_medecin_reference_rss_code,
      t_medecins_c.specialite_interne_medecin_reference_texte AS specialite_interne_medecin_reference_rss_texte,
      t_medecins_c.medecin_reference_id AS medecin_reference_rss_id,
      t_medecins_c.medecin_reference_code AS medecin_reference_rss_code,
      t_medecins_c.medecin_reference_nom_prenom AS medecin_reference_rss_nom,
      t_medecins_c.section_id AS medecin_rss_section_id,
      t_medecins_c.section_code AS medecin_rss_section_code,
      t_medecins_c.section_texte AS medecin_rss_section_texte,
      t_ghm_c.groupe_activite_id,
      t_ghm_c.groupe_activite_code,
      t_ghm_c.groupe_activite_texte,
      p_rss.ghs_id,
          CASE
              WHEN substr(t_ghs.code_text, 1, 1) = ANY (ARRAY['I'::text, 'D'::text]) THEN t_ghs.code_text
              ELSE to_char(t_ghs.code, '0000MI'::text)
          END AS ghs_code,
      t_ghs.texte AS ghs_texte,
      p_rss.prestation_principale_id,
      p_rss.acte_principal_id,
          CASE
              WHEN t_finess.type_etablissement::text = ANY (ARRAY['2'::text, '3'::text]) THEN
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.borne_basse_public_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.borne_basse_public_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.borne_basse_public_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.borne_basse_public_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.borne_basse_public_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.borne_basse_public_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.borne_basse_public_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.borne_basse_public_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.borne_basse_public_9
                  ELSE 0::numeric
              END
              ELSE
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_basse_prive_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_basse_prive_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_basse_prive_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_basse_prive_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_basse_prive_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_basse_prive_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_basse_prive_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_basse_prive_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_basse_prive_9
                  ELSE 0::numeric
              END
          END AS ghs_borne_basse,
          CASE
              WHEN t_finess.type_etablissement::text = ANY (ARRAY['2'::text, '3'::text]) THEN
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.borne_haute_public_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.borne_haute_public_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.borne_haute_public_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.borne_haute_public_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.borne_haute_public_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.borne_haute_public_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.borne_haute_public_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.borne_haute_public_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.borne_haute_public_9
                  ELSE 0::numeric
              END
              ELSE
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_haute_prive_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_haute_prive_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_haute_prive_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_haute_prive_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_haute_prive_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_haute_prive_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_haute_prive_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_haute_prive_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_haute_prive_9
                  ELSE 0::numeric
              END
          END AS ghs_borne_haute,
          CASE
              WHEN t_finess.type_etablissement::text = ANY (ARRAY['2'::text, '3'::text]) THEN
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.dms_nationale_public_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.dms_nationale_public_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.dms_nationale_public_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.dms_nationale_public_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.dms_nationale_public_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.dms_nationale_public_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.dms_nationale_public_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.dms_nationale_public_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.dms_nationale_public_9
                  ELSE 0::numeric
              END
              ELSE
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.dms_nationale_prive_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.dms_nationale_prive_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.dms_nationale_prive_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.dms_nationale_prive_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.dms_nationale_prive_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.dms_nationale_prive_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.dms_nationale_prive_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.dms_nationale_prive_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.dms_nationale_prive_9
                  ELSE 0::numeric
              END
          END AS dms_nationale_ghs,
          CASE
              WHEN t_finess.type_etablissement::text = ANY (ARRAY['2'::text, '3'::text]) THEN p_rss.duree_sejour - round(
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.dms_nationale_public_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.dms_nationale_public_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.dms_nationale_public_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.dms_nationale_public_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.dms_nationale_public_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.dms_nationale_public_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.dms_nationale_public_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.dms_nationale_public_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.dms_nationale_public_9
                  ELSE 0::numeric
              END)
              ELSE p_rss.duree_sejour - round(
              CASE
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.dms_nationale_prive_1
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.dms_nationale_prive_2
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.dms_nationale_prive_3
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.dms_nationale_prive_4
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.dms_nationale_prive_5
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.dms_nationale_prive_6
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.dms_nationale_prive_7
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.dms_nationale_prive_8
                  WHEN p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.dms_nationale_prive_9
                  ELSE 0::numeric
              END)
          END AS ecart_dms_nationale_ghs,
      t_ghm_c.dms_1 AS dms_ghm_1,
      p_rss.duree_sejour - round(t_ghm_c.dms_1) AS ecart_dms_ghm_1,
      t_ghm_c.dms_2 AS dms_ghm_2,
      p_rss.duree_sejour - round(t_ghm_c.dms_2) AS ecart_dms_ghm_2,
      t_ghm_c.dms_3 AS dms_ghm_3,
      p_rss.duree_sejour - round(t_ghm_c.dms_3) AS ecart_dms_ghm_3,
      t_ghm_c.dms_hospidiag AS dms_ghm_hospidiag,
      p_rss.duree_sejour - round(t_ghm_c.dms_hospidiag) AS ecart_dms_ghm_hospidiag,
      p_rss.diagnostic_principal_id,
      p_rss.diagnostic_relie_id,
      p_rss.duree_sejour,
      p_rss.nb_rsa,
          CASE
              WHEN t_ghm_c.cmd_code::text <> '28'::text THEN 0::numeric
              WHEN p_rss.nb_seances = 0::numeric THEN 1::numeric
              ELSE p_rss.nb_seances
          END::numeric(5,0) AS nb_seances,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 0::numeric
              WHEN p_rss.duree_sejour = 0::numeric THEN p_rss.nb_rsa
              ELSE 0::numeric
          END AS nb_rsa_0n,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 0::numeric
              WHEN p_rss.duree_sejour = 1::numeric THEN p_rss.nb_rsa
              ELSE 0::numeric
          END AS nb_rsa_1n,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 0::numeric
              WHEN p_rss.duree_sejour = 2::numeric THEN p_rss.nb_rsa
              ELSE 0::numeric
          END AS nb_rsa_2n,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 0::numeric
              WHEN p_rss.duree_sejour > 2::numeric THEN p_rss.nb_rsa
              ELSE 0::numeric
          END AS nb_rsa_xn,
          CASE
              WHEN p_rss.base_remboursement = 0::numeric AND t_ghm_c.pole_oap_code::text <> 'AL'::text THEN 1
              ELSE 0
          END AS nb_factures_nulles,
      p_rss.base_remboursement,
      p_rss.sejour_facture,
      p_rsf_total.taux_remboursement,
      p_rsf_total.sejour_remboursable,
      p_rss.honoraires_factures,
      p_rsf_total.honoraires_remboursable,
      p_rss.t2a_facture,
      p_rss.dmi_facture,
      p_rss.phc_facture,
      p_rsf_total.participation_assure_avant_oc,
      p_rsf_total.sejour_remboursable_2,
      p_rsf_total.honoraires_remboursable_2,
      p_rsf_total.montant_dmi_ghs2006,
      p_rss.igs2,
      0 AS no_oid,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 'S'::bpchar::character varying::bpchar
              ELSE t_ghm_c.mco
          END AS mcos_code,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN t_ghm_c.cmd_texte
              ELSE
              CASE
                  WHEN t_ghm_c.mco = '*'::bpchar THEN 'ePMSI Hors GHM'::character varying
                  ELSE t_ghm_c.mco_texte
              END
          END AS mcos_texte,
          CASE
              WHEN t_ghm_c.cmd_code::text = '28'::text THEN 4
              ELSE
              CASE t_ghm_c.mco
                  WHEN '*'::bpchar THEN 0
                  WHEN 'M'::bpchar THEN 1
                  WHEN 'C'::bpchar THEN 2
                  WHEN 'O'::bpchar THEN 3
                  ELSE NULL::integer
              END
          END AS mcos_id,
      p_rss.rss_mere_id,
      p_rss.poids_bebe,
      p_rss.age_gestationnel,
      p_rss.date_dernieres_regles,
      p_rss.confirmation_codage,
      p_rss.type_machine_rdth,
      p_rss.type_dosimetrie,
      p_rss.numero_innovation,
      p_rss.avec_anesthesie,
      p_rsf_total.nb_ghsmin,
      p_rss.coefficient_prudentiel_theorique,
      p_rss.pole_id AS pole_principal_id,
      t_conversion_hp_hc.oid AS conversion_hp_hc_id,
      t_conversion_hp_hc.code AS conversion_hp_hc_code,
      t_conversion_hp_hc.texte AS conversion_hp_hc_texte,
      t_raac.oid AS raac_id,
      t_raac.code AS raac_code,
      t_raac.texte AS raac_texte,
          CASE
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) <> '28'::text AND (("substring"(t_ghm_c.code::text, 6, 1) = ANY (ARRAY['J'::text, 'T'::text])) OR p_rss.duree_sejour < 2::numeric) THEN '1'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) = '28'::text THEN '2'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND ("substring"(t_ghm_c.code::text, 6, 1) <> ALL (ARRAY['J'::text, 'T'::text])) AND p_rss.duree_sejour > 1::numeric THEN '3'::text
              ELSE '0'::text
          END AS type_sejour_ovalide,
          CASE
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) <> '28'::text AND (("substring"(t_ghm_c.code::text, 6, 1) = ANY (ARRAY['J'::text, 'T'::text])) OR p_rss.duree_sejour < 2::numeric) THEN '1'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) = '28'::text THEN '2'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND ("substring"(t_ghm_c.code::text, 6, 1) <> ALL (ARRAY['J'::text, 'T'::text])) AND p_rss.duree_sejour > 1::numeric THEN '3'::text
              ELSE '0'::text
          END AS type_sejour_ovalide_id,
          CASE
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) <> '28'::text AND (("substring"(t_ghm_c.code::text, 6, 1) = ANY (ARRAY['J'::text, 'T'::text])) OR p_rss.duree_sejour < 2::numeric) THEN '1'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) = '28'::text THEN '2'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND ("substring"(t_ghm_c.code::text, 6, 1) <> ALL (ARRAY['J'::text, 'T'::text])) AND p_rss.duree_sejour > 1::numeric THEN '3'::text
              ELSE '0'::text
          END AS type_sejour_ovalide_code,
          CASE
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) <> '28'::text AND (("substring"(t_ghm_c.code::text, 6, 1) = ANY (ARRAY['J'::text, 'T'::text])) OR p_rss.duree_sejour < 2::numeric) THEN 'GHM de niveau J ou T ou DS < 2j'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND "substring"(t_ghm_c.code::text, 1, 2) = '28'::text THEN 'CM 28'::text
              WHEN p_rss.en_cours = '0'::bpchar AND (t_ghm_c.cas_code <> ALL (ARRAY['*'::bpchar, '?'::bpchar])) AND ("substring"(t_ghm_c.code::text, 6, 1) <> ALL (ARRAY['J'::text, 'T'::text])) AND p_rss.duree_sejour > 1::numeric THEN 'Autres GHM'::text
              ELSE '0'::text
          END AS type_sejour_ovalide_texte,
          CASE
              WHEN t_ghm_c.code::text ~~* '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 p_rss.duree_sejour > 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.code::text ~~* '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 p_rss.duree_sejour > 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_prestations.code::text = 'ATU'::text THEN '1'::text
              ELSE NULL::text
          END AS activite_urgence,
      p_rsf_total.ca_ghs_sans_coefficient,
      p_rsf_total.matricule_assure,
      t_rescrit_tarif.oid AS rescrit_tarif_id,
      t_rescrit_tarif.code AS rescrit_tarif_code,
      t_rescrit_tarif.texte AS rescrit_tarif_texte,
      t_adm_prod_reshospi.oid AS adm_prod_reshospi_id,
      t_adm_prod_reshospi.code AS adm_prod_reshospi_code,
      t_adm_prod_reshospi.texte AS adm_prod_reshospi_texte,
      t_contexte_pat.oid AS contexte_pat_id,
      t_contexte_pat.code AS contexte_pat_code,
      t_contexte_pat.texte AS contexte_pat_texte,
      t_nb_interventions.oid AS nb_intervention_id,
      t_nb_interventions.code AS nb_intervention_code,
      t_nb_interventions.texte AS nb_intervention_texte,
      p_rss.ca_ghs_raac,
      t_np.oid as np_id,
      t_np.code as np_code,
      t_np.texte as np_texte,
      t_passage_urgences.oid as passage_urgences_id,
      t_passage_urgences.code as passage_urgences_code,
      t_passage_urgences.texte as passage_urgences_texte
     FROM pmsi.p_rss
       JOIN base.t_finess ON p_rss.finess = t_finess.code::bpchar
       JOIN pmsi.p_rsf_total ON p_rss.oid = p_rsf_total.rss_id
       JOIN pmsi.t_prestations ON p_rss.prestation_principale_id = t_prestations.oid
       JOIN pmsi.t_ghm_c ON p_rss.ghm_id = t_ghm_c.oid
       JOIN pmsi.t_ghs ON p_rss.ghs_id = t_ghs.oid
       JOIN pmsi.t_medecins_c ON p_rss.medecin_rss_id = t_medecins_c.oid
       JOIN pmsi.t_conversion_hp_hc ON t_conversion_hp_hc.oid = p_rss.conversion_hp_hc_id
       JOIN pmsi.t_raac ON t_raac.oid = p_rss.raac_id
       JOIN pmsi.t_rescrit_tarif ON t_rescrit_tarif.oid = p_rss.rescrit_tarif_id
       JOIN pmsi.t_adm_prod_reshospi ON t_adm_prod_reshospi.oid = p_rss.adm_prod_reshospi_id
       JOIN pmsi.t_contexte_pat ON t_contexte_pat.oid = p_rss.contexte_pat_id
       JOIN pmsi.t_nb_interventions ON t_nb_interventions.oid = p_rss.nb_intervention_id
       JOIN pmsi.t_np ON t_np.oid = p_rss.np_id
       JOIN pmsi.t_passage_urgences ON t_passage_urgences.code = p_rss.passage_urgences_code
       JOIN base.t_sexe ON p_rss.sexe = t_sexe.code
    WHERE p_rss.etat <> 'S'::bpchar;
