dep:
  - v_unites_medicales_2
src: |
  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,
      p_rss_rum.provenance AS provenance_rum,
      p_rss_rum.mode_sortie AS mode_sortie_rum,
      p_rss_rum.destination AS destination_rum,
      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,
      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,
      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,
      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_conversion_hp_hc ON t_conversion_hp_hc.oid = p_rss_rum.conversion_hp_hc_id
       JOIN pmsi.t_passage_urgences ON t_passage_urgences.code = p_rss_rum.passage_urgences_code
       LEFT JOIN pmsi.t_pole ON p_rss_rum.pole_id = t_pole.oid;