return: text
lang: plpgsql
src: |
  DECLARE	
      _owner text;
  BEGIN

      -- Récupération du propriétaire
      SELECT login INTO _owner FROM base.cti_user_credentials();

      -- Création des vues matérialisées du PMSI
      RAISE NOTICE '%','Création des vues matérialisées du PMSI';
      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_1';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rss_1') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_1';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rss_1;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rss_1') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rss_1';
              DROP VIEW IF EXISTS pmsi.v_rss_1;
      END IF;
      
      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rss_1';
      CREATE MATERIALIZED VIEW pmsi.v_rss_1 AS
      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,
              CASE
                  WHEN p_rss.traitement_epmsi = '00'::bpchar THEN 'Dossier en cours'::text
                  WHEN p_rss.traitement_epmsi = '11'::bpchar THEN 'Non traités. CMD 90 hors 90H02Z et 90H03Z'::text
                  WHEN p_rss.traitement_epmsi = '12'::bpchar THEN 'Non traités. Prestation inter_établissement'::text
                  WHEN p_rss.traitement_epmsi = '13'::bpchar THEN 'Non traités. GHS 9999'::text
                  WHEN p_rss.traitement_epmsi = '14'::bpchar THEN 'Sans titre'::text
                  WHEN p_rss.traitement_epmsi = '15'::bpchar THEN 'Cohérence RSF'::text
                  WHEN p_rss.traitement_epmsi = '21'::bpchar THEN 'Non valorisés. Pb chainage'::text
                  WHEN p_rss.traitement_epmsi = '22'::bpchar THEN 'Non valorisés. Pb variable bloquante'::text
                  WHEN p_rss.traitement_epmsi = '23'::bpchar THEN 'Non valorisés. Attente décision droits'::text
                  WHEN p_rss.traitement_epmsi = '24'::bpchar THEN 'Non valorisés. Non facturables AM'::text
                  WHEN p_rss.traitement_epmsi = '25'::bpchar THEN 'Non valorisés. Prélèvement organes patient décédé'::text
                  WHEN p_rss.traitement_epmsi = '26'::bpchar THEN 'Non valorisés. Cause inconnue (Valosej)'::text
                  WHEN p_rss.traitement_epmsi = '30'::bpchar THEN 'Valorisé'::text
                  WHEN p_rss.traitement_epmsi = '31'::bpchar THEN 'Valorisé (AME)'::text
                  WHEN p_rss.traitement_epmsi = '32'::bpchar THEN 'Valorisé (IVG)'::text
                  WHEN p_rss.traitement_epmsi = '33'::bpchar THEN 'Valorisé (SU)'::text
                  ELSE 'Code non répertorié'::text
              END AS traitement_epmsi_texte,
          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,
          t_unites_medicales.code AS unite_medicale_principale_code,
          t_unites_medicales.texte AS unite_medicale_principale_texte,
          t_unites_medicales.section_id AS unite_medicale_principale_section_id,
          t_unites_medicales.section_code AS unite_medicale_principale_section_code,
          t_unites_medicales.section_texte AS unite_medicale_principale_section_texte,
          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,
          t_prestations.code AS prestation_principale_code,
          t_prestations.texte AS prestation_principale_texte,
          t_prestations.type_facturation_section_id AS prestation_principale_section_id,
          t_prestations.type_facturation_section_code AS prestation_principale_section_code,
          t_prestations.type_facturation_section_texte AS prestation_principale_section_texte,
          p_rss.acte_principal_id,
          t_actes_c.code AS acte_principal_code,
          t_actes_c.texte_court AS acte_principal_texte,
          t_actes_c.section_id AS acte_principal_section_id,
          t_actes_c.section_code AS acte_principal_section_code,
          t_actes_c.section_texte AS acte_principal_section_texte,
              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,
          t_diagnostics_principaux.code AS diagnostic_principal_code,
          t_diagnostics_principaux.texte AS diagnostic_principal_texte,
          t_diagnostics_principaux.racine_id AS diagnostic_principal_racine_id,
          t_diagnostics_principaux.racine_code AS diagnostic_principal_racine_code,
          t_diagnostics_principaux.racine_texte AS diagnostic_principal_racine_texte,
          t_diagnostics_principaux.section_id AS diagnostic_principal_section_id,
          t_diagnostics_principaux.section_code AS diagnostic_principal_section_code,
          t_diagnostics_principaux.section_texte AS diagnostic_principal_section_texte,
          p_rss.diagnostic_relie_id,
          t_diagnostics_relies.code AS diagnostic_relie_code,
          t_diagnostics_relies.texte AS diagnostic_relie_texte,
          t_diagnostics_relies.racine_id AS diagnostic_relie_racine_id,
          t_diagnostics_relies.racine_code AS diagnostic_relie_racine_code,
          t_diagnostics_relies.racine_texte AS diagnostic_relie_racine_texte,
          t_diagnostics_relies.section_id AS diagnostic_relie_section_id,
          t_diagnostics_relies.section_code AS diagnostic_relie_section_code,
          t_diagnostics_relies.section_texte AS diagnostic_relie_section_texte,
          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,
          t_unites_medicales.type_autorisation AS type_autorisation_principale,
          p_rss.etat_sejour AS etat_sejour_id,
          t_etat_sejour.code AS etat_sejour_code,
          t_etat_sejour.texte AS etat_sejour_texte,
          t_types_autorisations.texte AS type_autorisation_principale_texte,
          p_rss_rum.unite_medicale_id AS premiere_unite_medicale_id,
          (p_rss_rum.unite_medicale_id::text || '_'::text) || p_rss_rum.unite_medicale AS premiere_unite_medicale_code,
          p_rss_rum.unite_medicale AS premiere_unite_medicale_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_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_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_prestations ON p_rss.prestation_principale_id = t_prestations.oid
          JOIN pmsi.t_medecins_c ON p_rss.medecin_rss_id = t_medecins_c.oid
          JOIN pmsi.t_diagnostics t_diagnostics_principaux ON p_rss.diagnostic_principal_id = t_diagnostics_principaux.oid
          JOIN pmsi.t_diagnostics t_diagnostics_relies ON p_rss.diagnostic_relie_id = t_diagnostics_relies.oid
          JOIN pmsi.t_unites_medicales ON p_rss.unite_medicale_principale_id = t_unites_medicales.oid
          JOIN pmsi.t_actes_c ON p_rss.acte_principal_id = t_actes_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
          LEFT JOIN pmsi.t_etat_sejour ON p_rss.etat_sejour::text = t_etat_sejour.code
          LEFT JOIN pmsi.p_rss_rum ON p_rss_rum.rss_id = p_rss.oid AND p_rss_rum.no_rum = 1::numeric
          JOIN pmsi.t_types_autorisations ON t_types_autorisations.code = t_unites_medicales.type_autorisation
          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::text
          JOIN base.t_sexe ON p_rss.sexe::text = t_sexe.code
      WHERE p_rss.etat <> 'S'::bpchar
      ;
  
      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rss_1 OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rss_1';
      CREATE INDEX IF NOT EXISTS mv_rss_1_rss_id ON pmsi.v_rss_1 (rss_id);
      CREATE INDEX IF NOT EXISTS mv_rss_1_patient_id ON pmsi.v_rss_1 (patient_id);
      CREATE INDEX IF NOT EXISTS mv_rss_1_date_entree ON pmsi.v_rss_1 (date_entree);
      CREATE INDEX IF NOT EXISTS mv_rss_1_date_sortie ON pmsi.v_rss_1 (date_sortie);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rss_1';
      ANALYSE pmsi.v_rss_1
      ;


      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_7';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rss_7') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_7';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rss_7;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rss_7') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rss_7';
              DROP VIEW IF EXISTS pmsi.v_rss_7;
      END IF;

      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rss_7';
      CREATE MATERIALIZED VIEW pmsi.v_rss_7 AS
      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.import_id,
          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.ghm_prefixe,
          t_ghm_c.ghm5_id,
          t_ghm_c.ghm5_code,
          t_ghm_c.ghm5_texte,
          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,
          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.nb_rum,
          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,
          p_rss.medecin_rss_id,
          t_ghm_c.groupe_activite_id,
          t_ghm_c.groupe_activite_code,
          t_ghm_c.groupe_activite_texte,
          p_rss.ghs_id,
          p_rss.prestation_principale_id,
          p_rss.acte_principal_id,
          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,
          p_rss.igs2,
          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,
              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_rss.honoraires_factures,
          p_rss.t2a_facture,
          p_rss.dmi_facture,
          p_rss.phc_facture,
          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_rss.coefficient_prudentiel_theorique,
          p_rsf_total.nb_ghsmin,
          p_rss.pole_id AS pole_principal_id,
          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,
          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_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 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 base.t_finess ON p_rss.finess = t_finess.code::bpchar
          JOIN pmsi.t_ghm_c ON p_rss.ghm_id = t_ghm_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_medecins_c ON p_rss.medecin_rss_id = t_medecins_c.oid
          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::text
          JOIN base.t_sexe ON p_rss.sexe::text = t_sexe.code
      WHERE p_rss.etat <> 'S'::bpchar;

      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rss_7 OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rss_7';
      CREATE INDEX IF NOT EXISTS mv_rss_7_rss_id ON pmsi.v_rss_7 (rss_id);
      CREATE INDEX IF NOT EXISTS mv_rss_7_patient_id ON pmsi.v_rss_7 (patient_id);
      CREATE INDEX IF NOT EXISTS mv_rss_7_date_entree ON pmsi.v_rss_7 (date_entree);
      CREATE INDEX IF NOT EXISTS mv_rss_7_date_sortie ON pmsi.v_rss_7 (date_sortie);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rss_7';
      ANALYZE pmsi.v_rss_7
      ;


      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_8';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rss_8') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_8';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rss_8;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rss_8') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rss_8';
              DROP VIEW IF EXISTS pmsi.v_rss_8;
      END IF;

      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rss_8';
      CREATE MATERIALIZED VIEW pmsi.v_rss_8
      AS
      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.import_id,
          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.ghm_prefixe,
          t_ghm_c.ghm5_id,
          t_ghm_c.ghm5_code,
          t_ghm_c.ghm5_texte,
          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,
          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.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,
          p_rss.medecin_rss_id,
          t_ghm_c.groupe_activite_id,
          t_ghm_c.groupe_activite_code,
          t_ghm_c.groupe_activite_texte,
          p_rss.ghs_id,
          p_rss.prestation_principale_id,
          p_rss.acte_principal_id,
          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,
          p_rss.etat_sejour AS etat_sejour_id,
          t_etat_sejour.code AS etat_sejour_code,
          t_etat_sejour.texte AS etat_sejour_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_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.code AS np_code,
          t_np.oid AS np_id,
          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_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
          LEFT JOIN pmsi.t_etat_sejour ON p_rss.etat_sejour::text = t_etat_sejour.code
          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::text
          JOIN base.t_sexe ON p_rss.sexe::text = t_sexe.code
      WHERE p_rss.etat <> 'S'::bpchar
      ;

      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rss_8 OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rss_8';
      CREATE INDEX IF NOT EXISTS mv_rss_8_rss_id ON pmsi.v_rss_8 (rss_id);
      CREATE INDEX IF NOT EXISTS mv_rss_8_patient_id ON pmsi.v_rss_8 (patient_id);
      CREATE INDEX IF NOT EXISTS mv_rss_8_date_entree ON pmsi.v_rss_8 (date_entree);
      CREATE INDEX IF NOT EXISTS mv_rss_8_date_sortie ON pmsi.v_rss_8 (date_sortie);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rss_8';
      ANALYZE pmsi.v_rss_8
      ;


      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_base';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rss_base') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_base';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rss_base;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rss_base') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rss_base';
              DROP VIEW IF EXISTS pmsi.v_rss_base;
      END IF;

      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rss_base';
      CREATE MATERIALIZED VIEW pmsi.v_rss_base
      AS
      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,
          CASE
              WHEN p_rss.traitement_epmsi = '00'::bpchar THEN 'Dossier en cours'::text
              WHEN p_rss.traitement_epmsi = '11'::bpchar THEN 'Non traités. CMD 90 hors 90H02Z et 90H03Z'::text
              WHEN p_rss.traitement_epmsi = '12'::bpchar THEN 'Non traités. Prestation inter_établissement'::text
              WHEN p_rss.traitement_epmsi = '13'::bpchar THEN 'Non traités. GHS 9999'::text
              WHEN p_rss.traitement_epmsi = '14'::bpchar THEN 'Sans titre'::text
              WHEN p_rss.traitement_epmsi = '15'::bpchar THEN 'Cohérence RSF'::text
              WHEN p_rss.traitement_epmsi = '21'::bpchar THEN 'Non valorisés. Pb chainage'::text
              WHEN p_rss.traitement_epmsi = '22'::bpchar THEN 'Non valorisés. Pb variable bloquante'::text
              WHEN p_rss.traitement_epmsi = '23'::bpchar THEN 'Non valorisés. Attente décision droits'::text
              WHEN p_rss.traitement_epmsi = '24'::bpchar THEN 'Non valorisés. Non facturables AM'::text
              WHEN p_rss.traitement_epmsi = '25'::bpchar THEN 'Non valorisés. Prélèvement organes patient décédé'::text
              WHEN p_rss.traitement_epmsi = '26'::bpchar THEN 'Non valorisés. Cause inconnue (Valosej)'::text
              WHEN p_rss.traitement_epmsi = '30'::bpchar THEN 'Valorisé'::text
              WHEN p_rss.traitement_epmsi = '31'::bpchar THEN 'Valorisé (AME)'::text
              WHEN p_rss.traitement_epmsi = '32'::bpchar THEN 'Valorisé (IVG)'::text
              WHEN p_rss.traitement_epmsi = '33'::bpchar THEN 'Valorisé (SU)'::text
              ELSE 'Code non répertorié'::text
          END AS traitement_epmsi_texte,
      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,
      p_rss.date_naissance,
      p_rss.age,
      p_rss.code_postal,
      p_rss.code_postal_id,
      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,
      p_rss.medecin_rss_id,
      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,
      p_rss.conversion_hp_hc_id AS conversion_hp_hc_id,
      p_rss.raac_id AS raac_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,
      p_rss.etat_sejour,
          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,
      p_rsf_total.ca_ghs_sans_coefficient,
      p_rsf_total.matricule_assure,
      p_rss.rescrit_tarif_id,
      p_rss.adm_prod_reshospi_id,
      p_rss.contexte_pat_id,
      p_rss.nb_intervention_id,
      p_rss.ca_ghs_raac,
      p_rss.np_id,
      p_rss.passage_urgences_code
      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_ghm_c ON p_rss.ghm_id = t_ghm_c.oid
       JOIN pmsi.t_ghs ON p_rss.ghs_id = t_ghs.oid
      WHERE p_rss.etat <> 'S'::bpchar;

      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rss_base OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rss_base';
      CREATE INDEX IF NOT EXISTS mv_rss_base_rss_id ON pmsi.v_rss_base (rss_id);
      CREATE INDEX IF NOT EXISTS mv_rss_base_patient_id ON pmsi.v_rss_base (patient_id);
      CREATE INDEX IF NOT EXISTS mv_rss_base_date_entree ON pmsi.v_rss_base (date_entree);
      CREATE INDEX IF NOT EXISTS mv_rss_base_date_sortie ON pmsi.v_rss_base (date_sortie);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rss_base';
      ANALYZE pmsi.v_rss_base
      ;

      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rsf_detail_rum_1';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rsf_detail_rum_1') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rsf_detail_rum_1';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rsf_detail_rum_1;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rsf_detail_rum_1') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rsf_detail_rum_1';
              DROP VIEW IF EXISTS pmsi.v_rsf_detail_rum_1;
      END IF;

      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rsf_detail_rum_1';
      CREATE MATERIALIZED VIEW pmsi.v_rsf_detail_rum_1 AS 
      SELECT p_rss_rum.rss_id,
          p_rss_rum.finess,
          p_rss_rum.no_rss,
          p_rss_rum.no_rum,
          p_rss_rum.date_entree AS date_entree_rum,
          p_rss_rum.date_sortie AS date_sortie_rum,
          p_rsf_detail.operation_lamda,
          p_rsf_detail.date_comptable,
          p_rss_rum.duree_sejour AS duree_sejour_rum,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN '0'::text
                  ELSE '1'::text
              END AS rum_valorise,
          p_rsf_detail.nature,
          p_rsf_detail.mt,
          p_rsf_detail.dmt,
          COALESCE(p_rsf_detail.date_debut, p_rss_rum.date_entree) AS date_debut,
          COALESCE(p_rsf_detail.date_fin, p_rss_rum.date_sortie) AS date_fin,
          COALESCE(p_rsf_detail.nombre, 0::numeric) AS nombre,
          COALESCE(p_rsf_detail.coefficient, 0::numeric) AS coefficient,
          COALESCE(p_rsf_detail.nombre * p_rsf_detail.coefficient, 0::numeric) AS nombre_coefficient,
          COALESCE(p_rsf_detail.prix_unitaire, 0::numeric) AS prix_unitaire,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
                  WHEN p_rsf_detail.base_remboursement_rum_sauf_transition IS NOT NULL THEN p_rsf_detail.base_remboursement_rum_sauf_transition
                  WHEN p_rsf_detail.coefficient_mco > 0::numeric THEN round(p_rsf_detail.base_remboursement_rum / p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp, 2)
                  ELSE p_rsf_detail.base_remboursement_rum
              END AS base_remboursement_rum_sauf_transition,
          COALESCE(p_rsf_detail.base_remboursement_rum, 0::numeric) AS base_remboursement_rum,
          COALESCE(p_rsf_detail.taux_remboursement, 0::numeric) AS taux_remboursement,
          COALESCE(p_rsf_detail.sejour_remboursable_rum, 0::numeric) AS sejour_remboursable_rum,
          COALESCE(p_rsf_detail.sejour_facture, 0::numeric) AS sejour_facture,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
                  WHEN t_prestations.compteur_rsf::text = '1'::text OR p_rsf_detail.ligne_t2a = '1'::bpchar THEN p_rsf_detail.sejour_facture
                  ELSE 0::numeric
              END AS t2a_facture,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
                  WHEN t_prestations.compteur_rsf::text = '3'::text THEN p_rsf_detail.sejour_facture
                  ELSE 0::numeric
              END AS dmi_facture,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
                  WHEN t_prestations.compteur_rsf::text = '7'::text THEN p_rsf_detail.sejour_facture
                  ELSE 0::numeric
              END AS phc_facture,
          COALESCE(p_rsf_detail.sejour_remboursable, 0::numeric) AS sejour_remboursable,
          COALESCE(p_rsf_detail.compteur, ''::bpchar) AS compteur,
          COALESCE(p_rsf_detail.ligne_t2a, ''::bpchar) AS ligne_t2a,
          COALESCE(p_rsf_detail.pec_fj, ''::bpchar) AS pec_fj,
          COALESCE(p_rsf_detail.pec_fj_id, 0::bigint) AS pec_fj_id,
          t_pec_fj.code AS pec_fj_code,
          t_pec_fj.texte AS pec_fj_texte,
          COALESCE(p_rsf_detail.coefficient_mco, 0::numeric) AS coefficient_mco,
          COALESCE(p_rsf_detail.coefficient_geo, 0::numeric) AS coefficient_geo,
          COALESCE(p_rsf_detail.sejour_remboursable_2, 0::numeric) AS sejour_remboursable_2,
          COALESCE(p_rsf_detail.ghs_id, 0::bigint) AS ghs_id,
          t_ghs.code_text AS ghs_code,
          t_ghs.texte AS ghs_texte,
          p_rsf_detail.sejour_rembourse_noemie,
          p_rsf_detail.nature_noemie,
          COALESCE(p_rsf_detail.prestation_id, 0::bigint) AS prestation_id,
          t_prestations.code AS prestation_code,
          t_prestations.texte_court AS prestation_texte,
          t_prestations.section_id AS prestation_section_id,
          t_prestations.section_code AS prestation_section_code,
          t_prestations.section_texte AS prestation_section_texte,
              CASE
                  WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient < 1::numeric THEN 'EXB'::text
                  WHEN t_prestations.code::text = 'EXH'::text THEN 'EXH'::text
                  ELSE ''::text
              END AS prestation_exb_exh,
          p_rss_rum.pole_id AS pole_rum_id,
          t_pole.code AS pole_rum_code,
          t_pole.texte AS pole_rum_texte,
          t_pole.section_id AS pole_rum_section_id,
          t_pole.section_code AS pole_rum_section_code,
          t_pole.section_texte AS pole_rum_section_texte,
          COALESCE(v_unites_medicales_2_rsf.oid, p_rss_rum.unite_medicale_id) AS unite_medicale_id,
          COALESCE(v_unites_medicales_2_rsf.code, v_unites_medicales_2.code) AS unite_medicale_code,
          COALESCE(v_unites_medicales_2_rsf.texte, v_unites_medicales_2.texte) AS unite_medicale_texte,
          COALESCE(v_unites_medicales_2_rsf.type_autorisation, v_unites_medicales_2.type_autorisation) AS type_autorisation,
          COALESCE(v_unites_medicales_2_rsf.type_autorisation_texte, v_unites_medicales_2.type_autorisation_texte) AS type_autorisation_texte,
          COALESCE(v_unites_medicales_2_rsf.section_id, v_unites_medicales_2.section_id) AS unite_medicale_section_id,
          COALESCE(v_unites_medicales_2_rsf.section_code, v_unites_medicales_2.section_code) AS unite_medicale_section_code,
          COALESCE(v_unites_medicales_2_rsf.section_texte, v_unites_medicales_2.section_texte) AS unite_medicale_section_texte,
          COALESCE(v_unites_medicales_2_rsf.finess_geographique, v_unites_medicales_2.finess_geographique) AS finess_geographique,
              CASE
                  WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
                  WHEN p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame IS NOT NULL THEN p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame
                  ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.prix_unitaire * p_rsf_detail.coefficient, 2)
              END AS base_remboursement_rum_sauf_transition_sauf_ame,
          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,
          p_rsf_detail.fides_detail,
          p_rsf_detail.fides_detail AS fides_detail_id,
          p_rsf_detail.fides_detail::text AS fides_detail_code,
              CASE
                  WHEN p_rsf_detail.fides_detail = 1 THEN 'Valorisé FIDES (ligne)'::text
                  WHEN p_rsf_detail.fides_detail = 0 THEN 'Non Valorisé FIDES (ligne)'::text
                  ELSE 'Non renseigné'::text
              END AS fides_detail_texte,
          p_rss_rum.medecin_rum_id,
          t_medecins_c.code AS medecin_rum_code,
          t_medecins_c.nom_prenom AS medecin_rum_nom,
          t_medecins_c.specialite_medecin_reference_id AS specialite_medecin_reference_rum_id,
          t_medecins_c.specialite_medecin_reference_code AS specialite_medecin_reference_rum_code,
          t_medecins_c.specialite_medecin_reference_texte AS specialite_medecin_reference_rum_texte,
          t_medecins_c.section_id AS medecin_rum_section_id,
          t_medecins_c.section_code AS medecin_rum_section_code,
          t_medecins_c.section_texte AS medecin_rum_section_texte
      FROM pmsi.p_rss_rum
          LEFT JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss_rum.rss_id AND p_rsf_detail.no_rum = p_rss_rum.no_rum AND p_rsf_detail.est_ligne_rum = '1'::bpchar
          JOIN pmsi.t_prestations ON t_prestations.oid = COALESCE(p_rsf_detail.prestation_id, 0::bigint)
          JOIN pmsi.t_ghs ON t_ghs.oid = COALESCE(p_rsf_detail.ghs_id, 0::bigint)
          JOIN pmsi.v_unites_medicales_2 ON v_unites_medicales_2.oid = p_rss_rum.unite_medicale_id
          JOIN pmsi.t_types_autorisations ON v_unites_medicales_2.type_autorisation = t_types_autorisations.code
          JOIN pmsi.t_disciplines_sae ON t_types_autorisations.discipline_sae_id = t_disciplines_sae.oid
          JOIN pmsi.t_conversion_hp_hc ON t_conversion_hp_hc.oid = p_rss_rum.conversion_hp_hc_id
          LEFT JOIN pmsi.v_unites_medicales_2 v_unites_medicales_2_rsf ON v_unites_medicales_2_rsf.oid = p_rsf_detail.unite_medicale_id AND p_rsf_detail.unite_medicale_id <> p_rss_rum.unite_medicale_id
          JOIN base.t_pec_fj ON t_pec_fj.oid = COALESCE(p_rsf_detail.pec_fj_id, 0::bigint)
          LEFT JOIN pmsi.t_pole ON p_rss_rum.pole_id = t_pole.oid
          JOIN pmsi.t_medecins_c ON p_rss_rum.medecin_rum_id = t_medecins_c.oid
      ;

      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rsf_detail_rum_1 OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rsf_detail_rum_1';
      CREATE INDEX IF NOT EXISTS mv_rsf_detail_rum_1_no_rss ON pmsi.v_rsf_detail_rum_1 (no_rss);
      CREATE INDEX IF NOT EXISTS mv_rsf_detail_rum_1_no_rum ON pmsi.v_rsf_detail_rum_1 (no_rum);
      CREATE INDEX IF NOT EXISTS mv_rsf_detail_rum_1_date_entree_rum ON pmsi.v_rsf_detail_rum_1 (date_entree_rum);
      CREATE INDEX IF NOT EXISTS mv_rsf_detail_rum_1_date_sortie_rum ON pmsi.v_rsf_detail_rum_1 (date_sortie_rum);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rsf_detail_rum_1';
      ANALYZE pmsi.v_rsf_detail_rum_1
      ;


      RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_rum_2';

      IF EXISTS (SELECT * FROM pg_matviews WHERE schemaname = 'pmsi' AND matviewname = 'v_rss_rum_2') THEN
          RAISE NOTICE '%','Suppression de la vue matérialisée pmsi.v_rss_rum_2';
              DROP MATERIALIZED VIEW IF EXISTS pmsi.v_rss_rum_2;
      END IF;
    
      IF EXISTS (SELECT * FROM pg_views WHERE schemaname = 'pmsi' AND viewname = 'v_rss_rum_2') THEN
              RAISE NOTICE '%','Suppression de la vue pmsi.v_rss_rum_2';
              DROP VIEW IF EXISTS pmsi.v_rss_rum_2;
      END IF;

      RAISE NOTICE '%','Création de la vue matérialisée pmsi.v_rss_rum_2';
      CREATE MATERIALIZED VIEW pmsi.v_rss_rum_2 AS
      SELECT DISTINCT p_rss_rum.rss_id,
          p_rss_rum.finess,
          p_rss_rum.no_rss,
          p_rss_rum.no_rum,
          p_rss_rum.date_entree AS date_entree_rum,
          p_rss_rum.date_sortie AS date_sortie_rum,
          p_rss_rum.mode_entree AS mode_entree_rum,
          t_modes_entree.texte AS mode_entree_rum_texte,
          t_modes_entree.texte_court AS mode_entree_rum_texte_court,
          p_rss_rum.provenance AS provenance_rum,
          t_provenances_entree.texte AS provenance_rum_texte,
          t_provenances_entree.texte_court AS provenance_rum_texte_court,
          p_rss_rum.mode_sortie AS mode_sortie_rum,
          t_modes_sortie.texte AS mode_sortie_rum_texte,
          t_modes_sortie.texte_court AS mode_sortie_rum_texte_court,
          p_rss_rum.destination AS destination_rum,
          t_destinations_sortie.texte AS destination_rum_texte,
          t_destinations_sortie.texte_court AS destination_rum_texte_court,
          p_rss_rum.unite_medicale_id,
          v_unites_medicales_2.code AS unite_medicale_code,
          v_unites_medicales_2.texte AS unite_medicale_texte,
          v_unites_medicales_2.type_autorisation,
          v_unites_medicales_2.type_autorisation_texte,
          v_unites_medicales_2.section_id AS unite_medicale_section_id,
          v_unites_medicales_2.section_code AS unite_medicale_section_code,
          v_unites_medicales_2.section_texte AS unite_medicale_section_texte,
          p_rss_rum.type_autorisation_lit_dedie,
          p_rss_rum.pole_id AS pole_rum_id,
          t_pole.code AS pole_rum_code,
          t_pole.texte AS pole_rum_texte,
          t_pole.section_id AS pole_rum_section_id,
          t_pole.section_code AS pole_rum_section_code,
          t_pole.section_texte AS pole_rum_section_texte,
          p_rss_rum.diagnostic_principal_id AS diagnostic_principal_rum_id,
          t_diagnostics_principaux.code AS diagnostic_principal_rum_code,
          t_diagnostics_principaux.texte AS diagnostic_principal_rum_texte,
          t_diagnostics_principaux.racine_id AS diagnostic_principal_rum_racine_id,
          t_diagnostics_principaux.racine_code AS diagnostic_principal_rum_racine_code,
          t_diagnostics_principaux.racine_texte AS diagnostic_principal_rum_racine_texte,
          p_rss_rum.diagnostic_relie_id AS diagnostic_relie_rum_id,
          t_diagnostics_relies.code AS diagnostic_relie_rum_code,
          t_diagnostics_relies.texte AS diagnostic_relie_rum_texte,
          1 AS nb_rum_rum,
              CASE
                  WHEN v_unites_medicales_2.type_autorisation <> ''::text THEN 1
                  ELSE 0
              END AS nb_rum_avec_autorisation,
              CASE
                  WHEN p_rss_rum.mode_entree = '0'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_entree_0,
              CASE
                  WHEN p_rss_rum.mode_entree = '6'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_entree_6,
              CASE
                  WHEN p_rss_rum.mode_entree = '7'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_entree_7,
              CASE
                  WHEN p_rss_rum.mode_entree = '8'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_entree_8,
              CASE
                  WHEN p_rss_rum.mode_sortie = '0'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_sortie_0,
              CASE
                  WHEN p_rss_rum.mode_sortie = '6'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_sortie_6,
              CASE
                  WHEN p_rss_rum.mode_sortie = '7'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_sortie_7,
              CASE
                  WHEN p_rss_rum.mode_sortie = '8'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_sortie_8,
              CASE
                  WHEN p_rss_rum.mode_sortie = '9'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_mode_sortie_9,
          p_rss_rum.duree_sejour AS duree_sejour_rum,
          p_rss_rum.igs2 AS igs2_rum,
          p_rss_rum.nb_seances AS nb_seances_rum,
              CASE
                  WHEN p_rss_rum.provenance = '1'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_1,
              CASE
                  WHEN p_rss_rum.provenance = '2'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_2,
              CASE
                  WHEN p_rss_rum.provenance = '3'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_3,
              CASE
                  WHEN p_rss_rum.provenance = '4'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_4,
              CASE
                  WHEN p_rss_rum.provenance = '5'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_5,
              CASE
                  WHEN p_rss_rum.provenance = '6'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_6,
              CASE
                  WHEN p_rss_rum.provenance = '7'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_7,
              CASE
                  WHEN p_rss_rum.provenance = '8'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_provenance_8,
              CASE
                  WHEN lower(p_rss_rum.provenance::text) = 'r'::text THEN 1
                  ELSE 0
              END AS nb_rum_provenance_r,
              CASE
                  WHEN p_rss_rum.destination = '1'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_1,
              CASE
                  WHEN p_rss_rum.destination = '2'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_2,
              CASE
                  WHEN p_rss_rum.destination = '3'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_3,
              CASE
                  WHEN p_rss_rum.destination = '4'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_4,
              CASE
                  WHEN p_rss_rum.destination = '5'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_5,
              CASE
                  WHEN p_rss_rum.destination = '6'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_6,
              CASE
                  WHEN p_rss_rum.destination = '7'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_7,
              CASE
                  WHEN p_rss_rum.destination = '8'::bpchar THEN 1
                  ELSE 0
              END AS nb_rum_destination_8,
              CASE
                  WHEN lower(p_rss_rum.destination::text) = 'r'::text THEN 1
                  ELSE 0
              END AS nb_rum_destination_r,
          COALESCE(v_unites_medicales_2.finess_geographique, p_rss_rum.finess::text) AS finess_geographique,
          t_modes_hospitalisation.oid AS mode_hospitalisation_id,
          t_modes_hospitalisation.code AS mode_hospitalisation_code,
          t_modes_hospitalisation.texte AS mode_hospitalisation_texte,
          t_disciplines_sae.oid AS discipline_sae_id,
          t_disciplines_sae.code AS discipline_sae_code,
          t_disciplines_sae.texte AS discipline_sae_texte,
          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,
          p_rss_rum.medecin_rum_id,
          t_medecins_c.code AS medecin_rum_code,
          t_medecins_c.nom_prenom AS medecin_rum_nom,
          t_medecins_c.specialite_medecin_reference_id AS specialite_medecin_reference_rum_id,
          t_medecins_c.specialite_medecin_reference_code AS specialite_medecin_reference_rum_code,
          t_medecins_c.specialite_medecin_reference_texte AS specialite_medecin_reference_rum_texte,
          t_medecins_c.section_id AS medecin_rum_section_id,
          t_medecins_c.section_code AS medecin_rum_section_code,
          t_medecins_c.section_texte AS medecin_rum_section_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_rum
          JOIN pmsi.v_unites_medicales_2 ON p_rss_rum.unite_medicale_id = v_unites_medicales_2.oid
          JOIN pmsi.t_diagnostics t_diagnostics_principaux ON p_rss_rum.diagnostic_principal_id = t_diagnostics_principaux.oid
          JOIN pmsi.t_types_autorisations ON v_unites_medicales_2.type_autorisation = t_types_autorisations.code
          JOIN pmsi.t_modes_hospitalisation ON t_types_autorisations.mode_hospitalisation_id = t_modes_hospitalisation.oid
          JOIN pmsi.t_disciplines_sae ON t_types_autorisations.discipline_sae_id = t_disciplines_sae.oid
          JOIN pmsi.t_modes_entree ON p_rss_rum.mode_entree = t_modes_entree.code
          JOIN pmsi.t_provenances_entree ON p_rss_rum.provenance = t_provenances_entree.code
          JOIN pmsi.t_modes_sortie ON p_rss_rum.mode_sortie = t_modes_sortie.code
          JOIN pmsi.t_destinations_sortie ON p_rss_rum.destination = t_destinations_sortie.code
          JOIN pmsi.t_diagnostics t_diagnostics_relies ON p_rss_rum.diagnostic_relie_id = t_diagnostics_relies.oid
          JOIN pmsi.t_conversion_hp_hc ON t_conversion_hp_hc.oid = p_rss_rum.conversion_hp_hc_id
          LEFT JOIN pmsi.t_pole ON p_rss_rum.pole_id = t_pole.oid
          JOIN pmsi.t_medecins_c ON p_rss_rum.medecin_rum_id = t_medecins_c.oid
          JOIN pmsi.t_passage_urgences ON t_passage_urgences.code = p_rss_rum.passage_urgences_code;

      -- Evaluation variable _owner nécessaire
      EXECUTE 'ALTER TABLE pmsi.v_rss_rum_2 OWNER TO ' || _owner;

      RAISE NOTICE '%','Création des index pour pmsi.v_rss_rum_2';
      CREATE INDEX IF NOT EXISTS mv_rss_rum_2_no_rss ON pmsi.v_rss_rum_2 (no_rss);
      CREATE INDEX IF NOT EXISTS mv_rss_rum_2_no_rum ON pmsi.v_rss_rum_2 (no_rum);
      CREATE INDEX IF NOT EXISTS mv_rss_rum_2_date_entree_rum ON pmsi.v_rss_rum_2 (date_entree_rum);
      CREATE INDEX IF NOT EXISTS mv_rss_rum_2_date_sortie_rum ON pmsi.v_rss_rum_2 (date_sortie_rum);
      
      RAISE NOTICE '%','Analyse de pmsi.v_rss_rum_2';
      ANALYZE pmsi.v_rss_rum_2
      ;

      RETURN 'OK';
  END;