dep:
  - v_unites_medicales_2
src: |
  SELECT p_rss_diagnostics.rss_id,
      p_rss_diagnostics.finess,
      p_rss_diagnostics.no_rss,
      p_rss_diagnostics.no_rum,
      p_rss_diagnostics.diagnostic_id,
      t_diagnostics.code AS diagnostic_code,
      t_diagnostics.texte AS diagnostic_texte,
      t_diagnostics.racine_id AS diagnostic_racine_id,
      t_diagnostics.racine_code AS diagnostic_racine_code,
      t_diagnostics.racine_texte AS diagnostic_racine_texte,
      t_diagnostics.section_id AS diagnostic_section_id,
      t_diagnostics.section_code AS diagnostic_section_code,
      t_diagnostics.section_texte AS diagnostic_section_texte,
      t_diagnostics.severite_fg11_simulation_code AS diagnostic_severite_fg11_simulation_code,
      t_diagnostics.severite_fg11_code AS diagnostic_severite_fg11_code,
      t_diagnostics.severite_fg12_code AS diagnostic_severite_fg12_code,
      t_diagnostics.severite_fg13_code AS diagnostic_severite_fg13_code,
      t_diagnostics.severite_fg1311d_code AS diagnostic_severite_fg1311d_code,
      t_diagnostics.severite_fg1311e_code AS diagnostic_severite_fg1311e_code,
      t_diagnostics.severite_fg1311f_code AS diagnostic_severite_fg1311f_code,
      t_diagnostics.severite_fg1311g_code AS diagnostic_severite_fg1311g_code,
      t_diagnostics.severite_fg2016_code AS diagnostic_severite_fg2016_code,
      t_diagnostics.severite_fg2017_code AS diagnostic_severite_fg2017_code,
      t_diagnostics.severite_fg2018_code AS diagnostic_severite_fg2018_code,
      t_diagnostics.severite_fg2019_code AS diagnostic_severite_fg2019_code,
      t_diagnostics.severite_fg2020_code AS diagnostic_severite_fg2020_code,
      t_diagnostics.severite_fg2021_code AS diagnostic_severite_fg2021_code,
      t_diagnostics.severite_fg2022_code AS diagnostic_severite_fg2022_code,
      t_diagnostics.severite_fg2023_code AS diagnostic_severite_fg2023_code,
      p_rss_diagnostics.type_diagnostic,
      p_rss_diagnostics.type_diagnostic_rss,
      p_rss_diagnostics.imprecis,
      p_rss_diagnostics.unite_medicale,
      p_rss_diagnostics.unite_medicale_id,
      p_rss_diagnostics.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,
      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,
      t_diagnostics.severite_fg11_simulation_code,
      1 AS nb_diagnostics,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic = 'DP'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_p,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic = 'DR'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_r,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic = 'DS'::bpchar OR p_rss_diagnostics.type_diagnostic = 'DA'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_a,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic_rss = 'DP'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_rss_p,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic_rss = 'DR'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_rss_r,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic_rss = 'DS'::bpchar OR p_rss_diagnostics.type_diagnostic_rss = 'DA'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_rss_a,
          CASE
              WHEN p_rss_diagnostics.type_diagnostic_rss = 'DD'::bpchar THEN 1
              ELSE 0
          END AS nb_diagnostics_rss_d
     FROM pmsi.p_rss_diagnostics p_rss_diagnostics,
      pmsi.t_diagnostics,
      pmsi.v_unites_medicales_2,
      pmsi.t_pole
    WHERE p_rss_diagnostics.diagnostic_id = t_diagnostics.oid AND p_rss_diagnostics.unite_medicale_id = v_unites_medicales_2.oid AND p_rss_diagnostics.pole_id = t_pole.oid;