dep:
  - v_listes_3
  - v_unites_medicales_2
src: |
  SELECT subview.rss_id,
      subview.date_acte,
      1 AS nb_interventions,
      subview.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,
      t_actes_c.nomenclature AS acte_principal_nomenclature,
      t_actes_c.ccam_regroupement_id_1 AS acte_principal_regroupement_id,
      t_actes_c.ccam_regroupement_code_1 AS acte_principal_regroupement_code,
      t_actes_c.ccam_regroupement_texte_1 AS acte_principal_regroupement_texte,
      subview.actes_id,
      subview.actes_code,
      t_medecins_c.specialite_id AS specialite_medecin_id,
      t_medecins_c.specialite_medecin_code,
      t_medecins_c.specialite_medecin_texte,
      t_medecins_c.specialite_interne_id AS specialite_interne_medecin_id,
      t_medecins_c.specialite_interne_medecin_code,
      t_medecins_c.specialite_interne_medecin_texte,
      subview.medecin_id,
      t_medecins_c.code AS medecin_code,
      t_medecins_c.nom_prenom AS medecin_nom,
      t_medecins_c.specialite_medecin_reference_id,
      t_medecins_c.specialite_medecin_code AS specialite_medecin_reference_code,
      t_medecins_c.specialite_medecin_texte AS specialite_medecin_reference_texte,
      t_medecins_c.specialite_interne_id AS specialite_interne_medecin_reference_id,
      t_medecins_c.specialite_interne_medecin_code AS specialite_interne_medecin_reference_code,
      t_medecins_c.specialite_interne_medecin_texte AS specialite_interne_medecin_reference_texte,
      t_medecins_c.medecin_reference_id,
      t_medecins_c.medecin_reference_code,
      t_medecins_c.medecin_reference_nom_prenom AS medecin_reference_nom,
      t_medecins_c.section_id AS medecin_reference_section_id,
      t_medecins_c.section_code AS medecin_reference_section_code,
      t_medecins_c.section_texte AS medecin_reference_section_texte,
      t_anesthesistes_c.specialite_id AS specialite_anesthesiste_id,
      t_anesthesistes_c.specialite_medecin_code AS specialite_anesthesiste_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.specialite_medecin_texte
              ELSE 'Sans anesthésie'::character varying
          END AS specialite_anesthesiste_texte,
      t_anesthesistes_c.specialite_interne_id AS specialite_interne_anesthesiste_id,
      t_anesthesistes_c.specialite_interne_medecin_code AS specialite_interne_anesthesiste_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.specialite_interne_medecin_texte
              ELSE 'Sans anesthésie'::character varying
          END AS specialite_interne_anesthesiste_texte,
      subview.anesthesiste_id,
      t_anesthesistes_c.code AS anesthesiste_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.nom_prenom
              ELSE 'Sans anesthésie'::text
          END AS anesthesiste_nom,
      t_anesthesistes_c.specialite_medecin_reference_id AS specialite_anesthesiste_reference_id,
      t_anesthesistes_c.specialite_medecin_code AS specialite_anesthesiste_reference_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.specialite_medecin_texte
              ELSE 'Sans anesthésie'::character varying
          END AS specialite_anesthesiste_reference_texte,
      t_anesthesistes_c.specialite_interne_id AS specialite_interne_anesthesiste_reference_id,
      t_anesthesistes_c.specialite_interne_medecin_code AS specialite_interne_anesthesiste_reference_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.specialite_interne_medecin_texte
              ELSE 'Sans anesthésie'::character varying
          END AS specialite_interne_anesthesiste_reference_texte,
      t_anesthesistes_c.medecin_reference_id AS anesthesiste_reference_id,
      t_anesthesistes_c.medecin_reference_code AS anesthesiste_reference_code,
          CASE
              WHEN subview.avec_anesthesie = '1'::text THEN t_anesthesistes_c.medecin_reference_nom_prenom
              ELSE 'Sans anesthésie'::text
          END AS anesthesiste_reference_nom,
      t_anesthesistes_c.section_id AS anesthesiste_reference_section_id,
      t_anesthesistes_c.section_code AS anesthesiste_reference_section_code,
      t_anesthesistes_c.section_texte AS anesthesiste_reference_section_texte,
      subview.icr,
      subview.icr_1,
      subview.icr_4,
      subview.icr_prive_atih,
      subview.icr_prive_atih_1,
      subview.icr_prive_atih_4,
      subview.icr_public_atih,
      subview.icr_public_atih_1,
      subview.icr_public_atih_4,
      subview.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,
      subview.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,
      subview.unite_fonctionnelle_id,
      t_unites_fonctionnelles.code AS unite_fonctionnelle_code,
      t_unites_fonctionnelles.texte AS unite_fonctionnelle_texte,
      t_unites_fonctionnelles.section_id AS unite_fonctionnelle_section_id,
      t_unites_fonctionnelles.section_code AS unite_fonctionnelle_section_code,
      t_unites_fonctionnelles.section_texte AS unite_fonctionnelle_section_texte,
      subview.acte_avec_anesthesie
     FROM ( SELECT subview_1.rss_id,
              subview_1.date_acte,
              min(subview_1.unite_medicale_id) AS unite_medicale_id,
              min(subview_1.pole_id) AS pole_id,
              substr(max(
                  CASE
                      WHEN subview_1.acte_regroupement_code <> 'ADA'::text THEN to_char(subview_1.icr, '000000009'::text) || subview_1.acte_id::text
                      ELSE ' 0000000000'::text
                  END), 11)::bigint AS acte_principal_id,
              base.cti_group_concat(DISTINCT subview_1.acte_code) AS actes_code,
              base.cti_group_array(DISTINCT subview_1.acte_id::text) AS actes_id,
              substr(max(
                  CASE
                      WHEN subview_1.acte_regroupement_code <> 'ADA'::text THEN (to_char(subview_1.icr, '000000009'::text) || to_char(subview_1.medecin_id, '000000009'::text)) || subview_1.medecin_id::text
                      ELSE ' 000000000 0000000000'::text
                  END), 21)::bigint AS medecin_id,
              substr(max(
                  CASE
                      WHEN subview_1.acte_regroupement_code = 'ADA'::text THEN (to_char(subview_1.icr, '000000009'::text) || to_char(subview_1.medecin_id, '000000009'::text)) || subview_1.medecin_id::text
                      ELSE ' 000000000 0000000000'::text
                  END), 21)::bigint AS anesthesiste_id,
              sum(subview_1.icr) AS icr,
              sum(subview_1.icr_1) AS icr_1,
              sum(subview_1.icr_4) AS icr_4,
              sum(subview_1.icr_prive_atih) AS icr_prive_atih,
              sum(subview_1.icr_prive_atih_1) AS icr_prive_atih_1,
              sum(subview_1.icr_4) AS icr_prive_atih_4,
              sum(subview_1.icr_public_atih) AS icr_public_atih,
              sum(subview_1.icr_public_atih_1) AS icr_public_atih_1,
              sum(subview_1.icr_4) AS icr_public_atih_4,
              max(
                  CASE
                      WHEN subview_1.acte_regroupement_code = 'ADA'::text THEN '1'::text
                      ELSE '0'::text
                  END) AS avec_anesthesie,
              max(subview_1.unite_fonctionnelle_id) AS unite_fonctionnelle_id,
              max(subview_1.acte_avec_anesthesie) AS acte_avec_anesthesie
             FROM ( SELECT p_rss_actes.rss_id,
                      p_rss_actes.date_acte,
                      p_rss_actes.acte_id,
                      p_rss_actes.unite_medicale_id,
                      p_rss_actes.pole_id,
                      t_actes_c_1.code AS acte_code,
                      p_rss_actes.activite_ccam,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN t_actes_c_1.ccam_regroupement_code_1
                              ELSE t_actes_c_1.ccam_regroupement_code_4
                          END AS acte_regroupement_code,
                      p_rss_actes.medecin_id,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_1
                              ELSE p_rss_actes.nombre * t_actes_c_1.icr_4
                          END AS icr,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_1
                              ELSE 0::numeric
                          END AS icr_1,
                          CASE
                              WHEN p_rss_actes.activite_ccam = '4'::bpchar OR t_actes_c_1.ccam_regroupement_code_1 = 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_4
                              ELSE 0::numeric
                          END AS icr_4,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_prive_atih_1
                              ELSE p_rss_actes.nombre * t_actes_c_1.icr_prive_atih_4
                          END AS icr_prive_atih,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_prive_atih_1
                              ELSE 0::numeric
                          END AS icr_prive_atih_1,
                          CASE
                              WHEN p_rss_actes.activite_ccam = '4'::bpchar OR t_actes_c_1.ccam_regroupement_code_1 = 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_prive_atih_4
                              ELSE 0::numeric
                          END AS icr_prive_atih_4,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_public_atih_1
                              ELSE p_rss_actes.nombre * t_actes_c_1.icr_public_atih_4
                          END AS icr_public_atih,
                          CASE
                              WHEN p_rss_actes.activite_ccam <> '4'::bpchar AND t_actes_c_1.ccam_regroupement_code_1 <> 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_public_atih_1
                              ELSE 0::numeric
                          END AS icr_public_atih_1,
                          CASE
                              WHEN p_rss_actes.activite_ccam = '4'::bpchar OR t_actes_c_1.ccam_regroupement_code_1 = 'ADA'::text THEN p_rss_actes.nombre * t_actes_c_1.icr_public_atih_4
                              ELSE 0::numeric
                          END AS icr_public_atih_4,
                      p_rss_actes.unite_fonctionnelle_id,
                      p_rss_actes.acte_avec_anesthesie
                     FROM pmsi.p_rss_actes,
                      pmsi.t_actes_c t_actes_c_1
                    WHERE p_rss_actes.acte_id = t_actes_c_1.oid AND t_actes_c_1.nomenclature = 'CCAM'::text AND ((p_rss_actes.acte_id IN ( SELECT v_listes_3.to_id
                             FROM pmsi.v_listes_3
                            WHERE v_listes_3.liste_code = 'CTI_INTERV'::text)) OR p_rss_actes.activite_ccam = '4'::bpchar OR t_actes_c_1.ccam_regroupement_code_1 = 'ADA'::text)) subview_1
            GROUP BY subview_1.rss_id, subview_1.date_acte
           HAVING max(
                  CASE
                      WHEN subview_1.activite_ccam::text <> '4'::text THEN 1
                      ELSE 0
                  END) > 0) subview
       JOIN pmsi.t_actes_c ON subview.acte_principal_id = t_actes_c.oid
       JOIN pmsi.t_medecins_c ON subview.medecin_id = t_medecins_c.oid
       JOIN pmsi.t_medecins_c t_anesthesistes_c ON subview.anesthesiste_id = t_anesthesistes_c.oid
       JOIN pmsi.t_unites_fonctionnelles ON subview.unite_fonctionnelle_id = t_unites_fonctionnelles.oid
       JOIN pmsi.v_unites_medicales_2 ON subview.unite_medicale_id = v_unites_medicales_2.oid
       LEFT JOIN pmsi.t_pole ON subview.pole_id = t_pole.oid
    ORDER BY subview.rss_id, subview.date_acte;