src: |
  WITH w_indicateurs AS (
           SELECT t_indicateurs.oid AS indicateur_id,
                  CASE
                      WHEN t_indicateurs.table_name::text = '*RAPPORTCALC'::text THEN '*CALC'::text || t_rapports_rubriques.calc_formula
                      ELSE t_indicateurs.code
                  END ||
                  CASE
                      WHEN t_rapports_rubriques.indicateur_condition_id[1] > 0 THEN '#'::text || t_rapports_rubriques.indicateur_condition_id[1]::text
                      ELSE ''::text
                  END AS indicateur_code,
                  CASE
                      WHEN t_rapports_rubriques.entete_etendue::text <> ''::text THEN t_rapports_rubriques.entete_etendue::text::character varying
                      ELSE t_indicateurs.entete_etendue
                  END AS indicateur_entete_etendue,
              max(
                  CASE
                      WHEN t_rapports_rubriques.output_format <> ''::text THEN t_rapports_rubriques.output_format::character varying
                      ELSE t_indicateurs.output_format
                  END::text) AS indicateur_output_format,
              (min(ARRAY[
                  CASE
                      WHEN t_rapports.code ~~ 'CTI%'::text THEN '0'::text
                      ELSE '1'::text
                  END, t_rapports.code]))[2] AS rapport_code,
              (min(ARRAY[
                  CASE
                      WHEN t_rapports.code ~~ 'CTI%'::text THEN '0'::text
                      ELSE '1'::text
                  END, t_rapports.code, t_rapports.texte::text]))[3] AS rapport_texte,
              (min(ARRAY[
                  CASE
                      WHEN t_rapports.code ~~ 'CTI%'::text THEN '0'::text
                      ELSE '1'::text
                  END, t_rapports.code, to_char(t_rapports_rubriques.numero_rubrique, 'FM00000'::text)]))[3]::numeric AS numero_rubrique
             FROM eco.t_indicateurs
               JOIN eco.t_rapports_rubriques ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid
               JOIN eco.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
            WHERE t_rapports.code ~~ 'CTI\_VIEW\_%'::text AND t_rapports_rubriques.calc_formula !~~ '\%T(%'::text
            GROUP BY t_indicateurs.oid, (
                  CASE
                      WHEN t_indicateurs.table_name::text = '*RAPPORTCALC'::text THEN '*CALC'::text || t_rapports_rubriques.calc_formula
                      ELSE t_indicateurs.code
                  END ||
                  CASE
                      WHEN t_rapports_rubriques.indicateur_condition_id[1] > 0 THEN '#'::text || t_rapports_rubriques.indicateur_condition_id[1]::text
                      ELSE ''::text
                  END), (
                  CASE
                      WHEN t_rapports_rubriques.entete_etendue::text <> ''::text THEN t_rapports_rubriques.entete_etendue::text::character varying
                      ELSE t_indicateurs.entete_etendue
                  END)
            ORDER BY ((min(ARRAY[
                  CASE
                      WHEN t_rapports.code ~~ 'CTI%'::text THEN '0'::text
                      ELSE '1'::text
                  END, t_rapports.code]))[2]), ((min(ARRAY[
                  CASE
                      WHEN t_rapports.code ~~ 'CTI%'::text THEN '0'::text
                      ELSE '1'::text
                  END, t_rapports.code, to_char(t_rapports_rubriques.numero_rubrique, 'FM00000'::text)]))[3]::numeric)
          )
   SELECT substr(w_indicateurs.rapport_code, 10, 3) AS contexte,
      w_indicateurs.rapport_code,
      0::numeric AS numero_rubrique,
      (min(ARRAY[w_indicateurs.numero_rubrique::text, w_indicateurs.indicateur_code]))[2] AS code,
      w_indicateurs.rapport_texte AS texte,
      1::numeric AS level,
      ('VIEW.CONTEXTE=='''::text || substr(w_indicateurs.rapport_code, 10, 3)) || ''''::text AS condition,
      substr(w_indicateurs.rapport_code, 10, 3) || ''''::text AS viewtype,
      ''::text AS alias
     FROM w_indicateurs
    WHERE w_indicateurs.rapport_code <> ('CTI_VIEW_'::text || substr(w_indicateurs.rapport_code, 10, 3))
    GROUP BY (substr(w_indicateurs.rapport_code, 10, 3)), w_indicateurs.rapport_code, w_indicateurs.rapport_texte
  UNION
   SELECT substr(w_indicateurs.rapport_code, 10, 3) AS contexte,
      w_indicateurs.rapport_code,
      w_indicateurs.numero_rubrique * 10::numeric AS numero_rubrique,
      w_indicateurs.indicateur_code AS code,
      w_indicateurs.indicateur_entete_etendue AS texte,
          CASE
              WHEN w_indicateurs.rapport_code <> ('CTI_VIEW_'::text || substr(w_indicateurs.rapport_code, 10, 3)) THEN 2::numeric
              ELSE 1::numeric
          END AS level,
      ('VIEW.CONTEXTE=='''::text || substr(w_indicateurs.rapport_code, 10, 3)) || ''''::text AS condition,
      substr(w_indicateurs.rapport_code, 10, 3) || ''''::text AS viewtype,
          CASE
              WHEN row_number() OVER (PARTITION BY (substr(w_indicateurs.rapport_code, 10, 3)) ORDER BY w_indicateurs.numero_rubrique) = 1 THEN '*DFT'::text
              ELSE ''::text
          END AS alias
     FROM w_indicateurs
  UNION
   SELECT substr(w_indicateurs.rapport_code, 10, 3) AS contexte,
      w_indicateurs.rapport_code,
      w_indicateurs.numero_rubrique * 10::numeric + 1::numeric AS numero_rubrique,
      ('*CALC%T(['::text || w_indicateurs.indicateur_code) || '])'::text AS code,
      w_indicateurs.indicateur_entete_etendue::text || ' (Ratio % / Total)'::text AS texte,
          CASE
              WHEN w_indicateurs.rapport_code <> ('CTI_VIEW_'::text || substr(w_indicateurs.rapport_code, 10, 3)) THEN 3::numeric
              ELSE 2::numeric
          END AS level,
      ('VIEW.CONTEXTE=='''::text || substr(w_indicateurs.rapport_code, 10, 3)) || ''' && VIEW.VIEWTYPE==''ANZ'''::text AS condition,
      'ANZ'::text AS viewtype,
      ''::text AS alias
     FROM w_indicateurs
    WHERE w_indicateurs.indicateur_entete_etendue::text !~~ '%\%%'::text AND w_indicateurs.indicateur_output_format !~~ '%\%%'::text
    ORDER BY 1, 2, 3;