src: |
  SELECT t_budgets.oid,
      t_budgets_saisis.parent_id,
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.code
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.code
              ELSE ''::text
          END AS parent_code,
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.texte
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.texte
              ELSE ''::text
          END AS parent_texte,
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.section_id
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.section_id
              ELSE NULL::bigint[]
          END AS parent_section_id,
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.section_code
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.section_code
              ELSE NULL::text[]
          END AS parent_section_code,
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.section_texte
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.section_texte
              ELSE NULL::text[]
          END AS parent_section_texte,
      COALESCE(
          CASE
              WHEN t_budgets.dimension1 = 'UF'::text THEN t_unites_fonctionnelles.finess_id
              WHEN t_budgets.dimension1 = 'SER'::text THEN t_services_facturation.finess_id
              ELSE NULL::bigint
          END, 0::bigint) AS finess_id,
      COALESCE(t_finess_c.code, ''::text) AS finess_code,
      COALESCE(t_finess_c.texte, ''::text) AS finess_texte,
      t_finess_c.section_id AS finess_section_id,
      t_finess_c.section_code AS finess_section_code,
      t_finess_c.section_texte AS finess_section_texte,
      p_calendrier_mois.date_fin AS date,
      t_budgets_saisis.budget_nb_entrees_directes_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_entrees_directes,
      t_budgets_saisis.budget_nb_sorties_directes_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_sorties_directes,
      t_budgets_saisis.budget_nb_ambulatoires_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_ambulatoires,
      t_budgets_saisis.budget_nb_externes_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_externes,
      t_budgets_saisis.budget_nb_jours_js_non_inclus_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_jours_js_non_inclus,
      t_budgets_saisis.budget_nb_seances_mois[date_part('month'::text, p_calendrier_mois.date_fin)] AS nb_seances
     FROM activite.t_budgets
       JOIN base.p_calendrier_mois ON p_calendrier_mois.date_fin >= t_budgets.date_debut AND p_calendrier_mois.date_fin <= t_budgets.date_fin
       JOIN activite.t_budgets_saisis ON t_budgets_saisis.budget_id = t_budgets.oid
       LEFT JOIN activite.t_unites_fonctionnelles ON t_budgets_saisis.parent_id = t_unites_fonctionnelles.oid AND t_budgets.dimension1 = 'UF'::text
       LEFT JOIN activite.t_services_facturation ON t_budgets_saisis.parent_id = t_services_facturation.oid AND t_budgets.dimension1 = 'SER'::text
       LEFT JOIN activite.t_finess_c ON t_finess_c.oid = COALESCE(t_unites_fonctionnelles.finess_id, t_services_facturation.finess_id)
    ORDER BY t_budgets.oid, t_budgets_saisis.parent_code, t_budgets_saisis.parent_section_id;